﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using NUnit;
using NUnit.Framework;
using wne.Context;
using wne.core;
using wne.models;

#region Groups
/*
 * GetAvaliableParents - Sletov +
 * GetGroupById  - Sletov +
 * DeleteParentForGroup- Sletov +
 * AddParentForGroup - Detenkov - Korpak+
 * UpdateGroup - Detenkov - Korpak+
 * RemoveGroup - Detenkov - Sletov +
 * AddGroup - Detenkov - Sletov +
*/
#endregion

#region Users
/*
 * SetUserIsAdmin - Korpak+
 * GetGroupsByUser  - Korpak+
 * RemoveGroupFromUser  - Korpak +
 * AddGroupToUser - Sletov +
 * GetUserById - Il'in +
 * AddUser - Sletov +
 * UpdateUser - Il'in +
 * RemoveUser - Il'in +
 * SetUserIsAdmin - Korpak
 * GetUserWithGroupsList - Korpak
 * GetAvaliableGroupsForUser - Korpak
*/
#endregion

#region Routes
/*
 * CreateRoute
 * UpdateRoute
 * DeleteRoute
 * GetRouteById
 * GetFilteredRoutes 
 */
#endregion



using wne.models.Filter;

namespace wne.Tests
{
    [TestFixture]
    public class AdminFacadeTest
    {
        #region UsersAndGroups

        #region AddUser
        [Test]
        public void AddUser_without_groups()
        {
            var adfacade = new core.AdminFacadeSql();
            Dictionary<int, string> dic = new Dictionary<int, string>();
            dic = adfacade.GetAvaliableParents(2123);
            UserModel userModel1 = new UserModel();
            userModel1.FirstName = "TestFN";
            userModel1.SecondName = "TestSN";
            userModel1.Surname = "TestSur";
            userModel1.UserName = "TestLog";
            userModel1.Phone = "88888";
            userModel1.UserEmail = "TestMail";
            userModel1.UserBd = "1.01.1991";

            int id1 = adfacade.AddUser(userModel1);

            using (var context = new tpksEntities())
            {
                User user1 = context.User.Include("Group").FirstOrDefault(x => x.User_ID.Equals(id1));
                Assert.IsTrue(user1.Admin_Flag == "0" && user1.Birth_Date.Day == 1 && user1.Birth_Date.Month == 1 &&
                    user1.Birth_Date.Year == 1991 && user1.First_Name == "TestFN" && user1.Last_Name == "TestSur" &&
                    user1.Second_Name == "TestSN" && user1.Login == "TestLog" && user1.ICQ == 88888 &&
                    user1.Password == "change".ToString() && user1.Group.Count == 0);
            }

            adfacade.RemoveUser(id1);
        }

        [Test]
        public void AddUser_with_groups_without_fields()
        {
            var adfacade = new core.AdminFacadeSql();
            int idGrT;
            using (var context = new tpksEntities())
            {
                Group grouTest = new Group();
                grouTest.Group_Name = "GrTestSpecify-Secret-Individual";
                grouTest.Parent_Flag = "0";
                context.Group.Add(grouTest);
                context.SaveChanges();
                idGrT = grouTest.Group_ID;
            }


            UserModel userModel1 = new UserModel();
            userModel1.FirstName = "TestFN";
            userModel1.SecondName = "TestSN";
            userModel1.Surname = "TestSur";
            userModel1.UserName = "TestLog";
            userModel1.Phone = "88888";
            userModel1.UserEmail = "TestMail";
            userModel1.UserBd = "1991-01-1";
            userModel1.UserGroups = new List<GroupModel>();

            GroupModel group1 = new GroupModel();
            group1.Name = "GrTestSpecify-Secret-Individual";
            group1.Id = idGrT;
            userModel1.UserGroups.Add(group1);

            int id1 = adfacade.AddUser(userModel1);

            using (var context = new tpksEntities())
            {
                var allusers = context.User.ToList();
                var user1 = context.User.Include("Group").FirstOrDefault(x => x.User_ID == id1);
                var db_group = context.Group.Include("User").FirstOrDefault(x => x.Group_ID == idGrT);
                var gr_list = user1.Group.ToList();
                Assert.IsTrue(user1.Admin_Flag == "0" && user1.Birth_Date.Day == 1 && user1.Birth_Date.Month == 1 &&
                              user1.Birth_Date.Year == 1991 && user1.First_Name == "TestFN" &&
                              user1.Last_Name == "TestSur" &&
                              user1.Second_Name == "TestSN" && user1.Login == "TestLog" && user1.ICQ == 88888 &&
                              user1.Password == "change".ToString() && user1.Group.Count == 1 &&
                              gr_list[0].Group_Name == "GrTestSpecify-Secret-Individual" && gr_list[0].Parent_Flag == "0");

            }
            adfacade.RemoveGroup(idGrT);
            adfacade.RemoveUser(id1);
        }

        [Test]
        public void AddUser_with_groups_with_fields()
        {
            var adfacade = new core.AdminFacadeSql();
            int idGrT, idFiT;
            using (var context = new tpksEntities())
            {
                Group grouTest = new Group();
                grouTest.Group_Name = "GrTestSpecify-Secret-Individual";
                grouTest.Parent_Flag = "0";
                context.Group.Add(grouTest);
                context.SaveChanges();
                idGrT = grouTest.Group_ID;

                Group_Field field1 = new Group_Field();
                field1.Field_Name = "SpecSecrField";
                field1.Group_ID = idGrT;
                field1.Field_Type = 1;
                field1.Field_Value = "";
                field1.Mandatory_Flag = "0";
                field1.User_Edit_Flag = "0";
                context.Group_Field.Add(field1);
                context.SaveChanges();
                idFiT = field1.Field_ID;

            }


            UserModel userModel1 = new UserModel();
            userModel1.FirstName = "TestFN";
            userModel1.SecondName = "TestSN";
            userModel1.Surname = "TestSur";
            userModel1.UserName = "TestLog";
            userModel1.Phone = "88888";
            userModel1.UserEmail = "TestMail";
            userModel1.UserBd = "1991-01-1";
            userModel1.UserGroups = new List<GroupModel>();

            GroupModel group1 = new GroupModel();
            group1.Name = "GrTestSpecify-Secret-Individual";
            group1.Id = idGrT;
            userModel1.UserGroups.Add(group1);

            int id1 = adfacade.AddUser(userModel1);

            using (var context = new tpksEntities())
            {
                var allusers = context.User.ToList();
                var user1 = context.User.Include("Group").FirstOrDefault(x => x.User_ID == id1);
                var db_group = context.Group.Find(idGrT);
                var db_ugpv = context.User_Grp_Prm_Value.FirstOrDefault(x => x.Group_Field.Field_ID == idFiT);
                Assert.IsTrue(user1.Admin_Flag == "0" && user1.Birth_Date.Day == 1 && user1.Birth_Date.Month == 1 &&
                              user1.Birth_Date.Year == 1991 && user1.First_Name == "TestFN" &&
                              user1.Last_Name == "TestSur" &&
                              user1.Second_Name == "TestSN" && user1.Login == "TestLog" && user1.ICQ == 88888 &&
                              user1.Password == "change".ToString() && user1.Group.Count == 1 && user1.Group.Contains(db_group) &&
                              user1.User_Grp_Prm_Value.Contains(db_ugpv) && db_ugpv.Field_Value == "");

            }
            adfacade.RemoveGroup(idGrT);
            adfacade.RemoveUser(id1);
        }
        #endregion

        #region AddGroupToUser
        [Test]
        public void AddGroupToUser_without_fields()
        {
            using (var context = new tpksEntities())
            {
                User user5 = new User();
                user5.First_Name = "Test";
                user5.Second_Name = "Test_SN";
                user5.Last_Name = "Test_LN";
                user5.Login = "TestLogin";
                user5.Password = "TestPass";
                user5.ICQ = 111111;
                user5.Profession = "TestProfes";
                user5.Birth_Date = new DateTime(1991, 1, 1);
                user5.Admin_Flag = "0";

                context.User.Add(user5);

                context.SaveChanges();

                int id = user5.User_ID;

                Group group4 = new Group();
                group4.Group_Name = "TestGroup1";
                group4.Parent_Flag = "0";
                context.Group.Add(group4);

                Group group5 = new Group();
                group5.Group_Name = "TestGroup2";
                group5.Parent_Flag = "0";
                context.Group.Add(group5);

                context.SaveChanges();

                int id_g1 = group4.Group_ID;
                int id_g2 = group5.Group_ID;

                var adfacade = new core.AdminFacadeSql();

                adfacade.AddGroupToUser(id, id_g1);
                adfacade.AddGroupToUser(id, id_g2);

                User user1 = context.User.Include("Group").FirstOrDefault(x => x.User_ID.Equals(id));
                Group group1 = context.Group.Find(id_g1);
                Group group2 = context.Group.Find(id_g2);

                Assert.IsTrue(user1.Group.Contains(group1) && user1.Group.Contains(group2));

                adfacade.RemoveGroup(id_g1);
                adfacade.RemoveGroup(id_g2);
                adfacade.RemoveUser(id);
            }
        }
        [Test]
        public void AddGroupToUser_with_fields()
        {
            using (var context = new tpksEntities())
            {
                User user5 = new User();
                user5.First_Name = "Test";
                user5.Second_Name = "Test_SN";
                user5.Last_Name = "Test_LN";
                user5.Login = "TestLogin";
                user5.Password = "TestPass";
                user5.ICQ = 111111;
                user5.Profession = "TestProfes";
                user5.Birth_Date = new DateTime(1991, 1, 1);
                user5.Admin_Flag = "0";

                context.User.Add(user5);

                context.SaveChanges();

                int id = user5.User_ID;

                Group group5 = new Group();
                group5.Group_Name = "TestGroup2";
                group5.Parent_Flag = "0";
                context.Group.Add(group5);

                context.SaveChanges();

                int id_g1 = group5.Group_ID;

                Group_Field field1 = new Group_Field();
                field1.Field_Name = "F1";
                field1.Group_ID = id_g1;
                field1.Field_Type = 1;
                field1.Field_Value = "";
                field1.Mandatory_Flag = "0";
                field1.User_Edit_Flag = "0";
                context.Group_Field.Add(field1);

                Group_Field field2 = new Group_Field();
                field2.Field_Name = "F2";
                field2.Group_ID = id_g1;
                field2.Field_Type = 2;
                field2.Field_Value = "";
                field2.Mandatory_Flag = "0";
                field2.User_Edit_Flag = "0";
                context.Group_Field.Add(field2);

                Group_Field field3 = new Group_Field();
                field3.Field_Name = "F3";
                field3.Group_ID = id_g1;
                field3.Field_Type = 3;
                field3.Field_Value = "";
                field3.Mandatory_Flag = "0";
                field3.User_Edit_Flag = "0";
                context.Group_Field.Add(field3);

                Group_Field field4 = new Group_Field();
                field4.Field_Name = "F4";
                field4.Group_ID = id_g1;
                field4.Field_Type = 4;
                field4.Field_Value = "";
                field4.Mandatory_Flag = "0";
                field4.User_Edit_Flag = "0";
                context.Group_Field.Add(field4);

                Group_Field field5 = new Group_Field();
                field5.Field_Name = "F5";
                field5.Group_ID = id_g1;
                field5.Field_Type = 5;
                field5.Field_Value = "";
                field5.Mandatory_Flag = "0";
                field5.User_Edit_Flag = "0";
                context.Group_Field.Add(field5);

                Group_Field field6 = new Group_Field();
                field6.Field_Name = "F6";
                field6.Group_ID = id_g1;
                field6.Field_Type = 6;
                field6.Field_Value = "";
                field6.Mandatory_Flag = "0";
                field6.User_Edit_Flag = "0";
                context.Group_Field.Add(field6);

                context.SaveChanges();

                int id_f1 = field1.Field_ID;
                int id_f2 = field2.Field_ID;
                int id_f3 = field3.Field_ID;
                int id_f4 = field4.Field_ID;
                int id_f5 = field5.Field_ID;
                int id_f6 = field6.Field_ID;

                var adfacade = new core.AdminFacadeSql();

                adfacade.AddGroupToUser(id, id_g1);

                User user1 = context.User.Include("Group").Include("User_Grp_Prm_Value").FirstOrDefault(x => x.User_ID.Equals(id));
                Group group2 = context.Group.Find(id_g1);

                var values = context.User_Grp_Prm_Value.ToList();

                int i = values.Count;

                Assert.IsTrue(user1.Group.Contains(group2) &&
                    values[i - 6].Field_ID == id_f1 && values[i - 6].Field_Value == "" && values[i - 6].User_ID == id &&
                    values[i - 5].Field_ID == id_f2 && values[i - 5].Field_Value == "" && values[i - 5].User_ID == id &&
                    values[i - 4].Field_ID == id_f3 && values[i - 4].Field_Value == "" && values[i - 4].User_ID == id &&
                    values[i - 3].Field_ID == id_f4 && values[i - 3].Field_Value == "" && values[i - 3].User_ID == id &&
                    values[i - 2].Field_ID == id_f5 && values[i - 2].Field_Value == "" && values[i - 2].User_ID == id &&
                    values[i - 1].Field_ID == id_f6 && values[i - 1].Field_Value == "" && values[i - 1].User_ID == id);

                adfacade.RemoveGroup(id_g1);
                adfacade.RemoveUser(id);
            }
        }

        #endregion

        #region SetUserIsAdmin
        [Test]
        public void SetUserIsAdmin_set_not_admin_as_admin()
        {
            var adfacade = new core.AdminFacadeSql();

            adfacade.SetUserIsAdmin(1051, true);

            using (var context = new tpksEntities())
            {
                User user1 = context.User.Find(1051);
                Assert.IsTrue(user1.Admin_Flag == "1");
            }
        }
        [Test]
        public void SetUserIsAdmin_set_admin_as_admin()
        {
            var adfacade = new core.AdminFacadeSql();

            adfacade.SetUserIsAdmin(1048, true);

            using (var context = new tpksEntities())
            {
                User user1 = context.User.Find(1048);
                Assert.IsTrue(user1.Admin_Flag == "1");
            }
        }
        [Test]
        public void SetUserIsAdmin_set_admin_as_not_admin()
        {
            var adfacade = new core.AdminFacadeSql();

            adfacade.SetUserIsAdmin(1048, false);

            using (var context = new tpksEntities())
            {
                User user1 = context.User.Find(1048);
                Assert.IsTrue(user1.Admin_Flag == "0");
            }
        }
        [Test]
        public void SetUserIsAdmin_set_not_admin_as_not_admin()
        {
            var adfacade = new core.AdminFacadeSql();

            adfacade.SetUserIsAdmin(1051, false);

            using (var context = new tpksEntities())
            {
                User user1 = context.User.Find(1051);
                Assert.IsTrue(user1.Admin_Flag == "0");
            }
        }
        #endregion

        #region RemoveGroupFromUser
        [Test]
        public void RemoveGroupFromUser_with_fields()
        {
            var adfacade = new core.AdminFacadeSql();
            adfacade.AddGroupToUser(1049, 1096);
            adfacade.RemoveGroupFromUser(1049, 1096);
            using (var context = new tpksEntities())
            {
                User user1 = context.User.Find(1049);
                Group group1 = context.Group.Find(1096);
                Assert.IsTrue(user1.Group.Count == 0 && user1.User_Grp_Prm_Value.Count == 0);
            }
        }

        [Test]
        public void RemoveGroupFromUser_user_not_in_group()
        {
            var adfacade = new core.AdminFacadeSql();
            adfacade.RemoveGroupFromUser(1049, 1096);
            using (var context = new tpksEntities())
            {
                User user1 = context.User.Find(1049);
                Group group1 = context.Group.Find(1096);
                Assert.IsTrue(user1.Group.Count == 0 && user1.User_Grp_Prm_Value.Count == 0);
            }
        }

        #endregion

        #region GetGroupById

        [Test]
        public void GetGroupById_is_exist_without_fields()
        {
            int id;
            using (var context = new tpksEntities())
            {
                Group group5 = new Group();
                group5.Group_Name = "TestGroup";
                group5.Parent_Flag = "0";
                context.Group.Add(group5);
                context.SaveChanges();
                id = group5.Group_ID;
            }

            var adfacade = new core.AdminFacadeSql();

            GroupModel group1 = adfacade.GetGroupById(id);

            Assert.IsTrue(group1.Id == id && group1.Name == "TestGroup" && group1.Fields.Count == 0 && group1.SelectedParents.Count == 0);

            adfacade.RemoveGroup(id);
        }
        [Test]
        public void GetGroupById_is_not_exists()
        {
            var adfacade = new core.AdminFacadeSql();
            Assert.IsNull(adfacade.GetGroupById(900));
        }
        [Test]
        public void GetGroupById_is_exists_with_fields()
        {
            var adfacade = new core.AdminFacadeSql();

            GroupModel group1 = adfacade.GetGroupById(1096);

            Assert.IsTrue(group1.Id == 1096 &&
                group1.Name == "Users" &&
                group1.Fields.Count == 2 &&
                group1.Fields[0].FieldId == 1076 && group1.Fields[0].Name == "Date_of_registration" && group1.Fields[0].Type == FieldTypeEnum.Date && group1.Fields[0].Value == null && group1.Fields[0].Mandatory == true && group1.Fields[0].UserCanEdit == false &&
                group1.Fields[1].FieldId == 1077 && group1.Fields[1].Name == "Ratig" && group1.Fields[1].Type == FieldTypeEnum.Numeric && group1.Fields[1].Value == null && group1.Fields[1].Mandatory == false && group1.Fields[1].UserCanEdit == false &&
                group1.SelectedParents.Count == 0
                );

        }

        [Test]
        public void GetGroupById_is_exists_with_parents()
        {
            var adfacade = new core.AdminFacadeSql();

            GroupModel group1 = adfacade.GetGroupById(1097);

            Assert.IsTrue(group1.Id == 1097 &&
                group1.Name == "Admins" &&
                group1.Fields.Count == 2 &&
                group1.Fields[0].FieldId == 1071 && group1.Fields[0].Name == "Number_of_baned_users" && group1.Fields[0].Type == FieldTypeEnum.Numeric && group1.Fields[0].Value == null && group1.Fields[0].Mandatory == false && group1.Fields[0].UserCanEdit == false &&
                group1.Fields[1].FieldId == 1072 && group1.Fields[1].Name == "Ratig" && group1.Fields[1].Type == FieldTypeEnum.Numeric && group1.Fields[1].Value == null && group1.Fields[1].Mandatory == false && group1.Fields[1].UserCanEdit == false &&
                group1.SelectedParents.Count == 1 && group1.SelectedParents[1098] == "www"
                );

        }



        #endregion

        #region GetUserById

        [Test]
        public void GetUserById_is_exists()
        {
            var adfacade = new core.AdminFacadeSql();
            UserModel user1 = adfacade.GetUserById(1050);
            Assert.IsTrue(user1.UserName == "User3" && user1.Id == 1050 && user1.FirstName == "Name3" && user1.SecondName == "Second_Name3" &&
                user1.Surname == "Last_Name3" && user1.Phone == "333333" && user1.UserBd == "03.03.1993 0:00:00" && user1.UserGroups.Count == 2 &&
                user1.UserGroups[0].Id == 1060 && user1.UserGroups[0].Name == "Users" && user1.UserGroups[0].Fields.Count == 0 &&
                user1.UserGroups[1].Id == 1062 && user1.UserGroups[1].Name == "All users" && user1.UserGroups[1].Fields.Count == 0);
        }

        [Test]
        public void GetUserById_is_not_exist()
        {
            var adfacade = new core.AdminFacadeSql();
            Assert.IsNull(adfacade.GetUserById(234500));
        }
        #endregion

        #region RemoveUser

        [Test]
        public void RemoveUser_is_exist()
        {
            using (var context = new tpksEntities())
            {
                User user3 = new User();
                user3.First_Name = "NameTest";
                user3.Second_Name = "Second_Name_Test";
                user3.Last_Name = "Last_Name_Test";
                user3.Login = "Login_test";
                user3.Password = "Pass_test";
                user3.ICQ = 333333;
                user3.Profession = "Prof_test";
                user3.Birth_Date = new DateTime(1993, 3, 3);
                user3.Admin_Flag = "0";
                context.User.Add(user3);
                context.SaveChanges();
                int id = user3.User_ID;
                
                User_Grp_Prm_Value userGrPrm = new User_Grp_Prm_Value();
                userGrPrm.User_ID = id;
                userGrPrm.Field_ID = 1003;
                userGrPrm.Field_Value = "testField";
                context.User_Grp_Prm_Value.Add(userGrPrm);
                context.SaveChanges();

                var adfacade = new AdminFacadeSql();
                adfacade.RemoveUser(id);

                var user = context.User.FirstOrDefault(x => x.User_ID == id);
                var userPrm = context.User_Grp_Prm_Value.FirstOrDefault(x => x.User_ID == id);

                Assert.IsTrue(user == null && userPrm == null);
            }
        }

        /* [Test]
         public void RemoveUser_is_not_exist()
         {
             using (var context = new tpksEntities())
             {
                 var adfacade = new AdminFacadeSql();
                 adfacade.RemoveUser(294823498);
                 Assert.
             }
         }*/
        #endregion

        #region GetFilteredGroupSmall

        [Test]
        public void GetFilteredGroupSmall_1()
        {
            var adfacade = new AdminFacadeSql();
            GroupSmallFilter filt1 = new GroupSmallFilter();
            filt1.Count = 4;
            filt1.OrderEnum = (GroupSmallOrderEnum)0;
            filt1.IsOrderDesc = null;
            filt1.StartIndex = 8;
            filt1.SearchString = "";

            List<GroupSmallModel> list1 = adfacade.GetFilteredGroupSmall(filt1);

            Assert.IsTrue(list1.Count == 4 &&
                list1[0].Id == 1096 && list1[0].Name == "Users" && list1[0].Count == 0 && list1[0].Level == 1 &&
                    list1[0].Fields[0].FieldId == 1069 && list1[0].Fields[0].Name == "Date_of_registration" && list1[0].Fields[0].UserCanEdit == false && list1[0].Fields[0].Mandatory == true && list1[0].Fields[0].Type == FieldTypeEnum.Date &&
                    list1[0].Fields[1].FieldId == 1070 && list1[0].Fields[1].Name == "Ratig" && list1[0].Fields[1].UserCanEdit == false && list1[0].Fields[1].Mandatory == false && list1[0].Fields[1].Type == FieldTypeEnum.Numeric &&
                list1[1].Id == 1097 && list1[1].Name == "Admins" && list1[1].Count == 0 && list1[1].Level == 1 &&
                    list1[1].Fields[0].FieldId == 1071 && list1[1].Fields[0].Name == "Number_of_baned_users" && list1[1].Fields[0].UserCanEdit == false && list1[1].Fields[0].Mandatory == false && list1[1].Fields[0].Type == FieldTypeEnum.Numeric &&
                    list1[1].Fields[1].FieldId == 1072 && list1[1].Fields[1].Name == "Ratig" && list1[1].Fields[1].UserCanEdit == false && list1[1].Fields[1].Mandatory == false && list1[1].Fields[1].Type == FieldTypeEnum.Numeric &&
                list1[2].Id == 1098 && list1[2].Name == "All users" && list1[2].Count == 0 && list1[2].Level == 0 &&
                    list1[2].Fields[0].FieldId == 1073 && list1[2].Fields[0].Name == "Date_of_last_visit" && list1[2].Fields[0].UserCanEdit == false && list1[2].Fields[0].Mandatory == true && list1[2].Fields[0].Type == FieldTypeEnum.Date &&
                list1[3].Id == 1099 && list1[3].Name == "Guests" && list1[3].Count == 0 && list1[3].Level == 1 &&
                    list1[3].Fields[0].FieldId == 1073 && list1[3].Fields[0].Name == "Number_of_visits" && list1[3].Fields[0].UserCanEdit == false && list1[3].Fields[0].Mandatory == true && list1[3].Fields[0].Type == FieldTypeEnum.Numeric

);
        }

        [Test]
        public void GetFilteredGroupSmall_2()
        {
            var adfacade = new AdminFacadeSql();
            GroupSmallFilter filt1 = new GroupSmallFilter();
            filt1.Count = 3;
            filt1.OrderEnum = (GroupSmallOrderEnum)1;
            filt1.IsOrderDesc = true;
            filt1.StartIndex = 7;
            filt1.SearchString = "user";

            List<GroupSmallModel> list1 = adfacade.GetFilteredGroupSmall(filt1);

            Assert.IsTrue(list1.Count == 2 &&
                list1[0].Id == 1100 && list1[0].Name == "Baned users" && list1[0].Count == 0 && list1[0].Level == 0 && list1[0].Fields.Count == 2 &&
                list1[1].Id == 1096 && list1[1].Name == "Users" && list1[1].Count == 3 && list1[1].Level == 1 && list1[1].Fields.Count == 6
);
        }

        [Test]
        public void GetFilteredGroupSmall_3()
        {
            var adfacade = new AdminFacadeSql();
            GroupSmallFilter filt1 = new GroupSmallFilter();
            filt1.Count = 2;
            filt1.OrderEnum = (GroupSmallOrderEnum)2;
            filt1.IsOrderDesc = true;
            filt1.StartIndex = 10;
            filt1.SearchString = "";

            List<GroupSmallModel> list1 = adfacade.GetFilteredGroupSmall(filt1);

            Assert.IsTrue(list1.Count == 4 &&
                list1[2].Id == 1098 && list1[2].Name == "All users" && list1[2].Count == 0 && list1[2].Level == 0 &&
                    list1[2].Fields[0].FieldId == 1073 && list1[2].Fields[0].Name == "Date_of_last_visit" && list1[2].Fields[0].UserCanEdit == false && list1[2].Fields[0].Mandatory == true && list1[2].Fields[0].Type == FieldTypeEnum.Date &&
                list1[3].Id == 1099 && list1[3].Name == "Guests" && list1[3].Count == 0 && list1[3].Level == 1 &&
                    list1[3].Fields[0].FieldId == 1073 && list1[3].Fields[0].Name == "Number_of_visits" && list1[3].Fields[0].UserCanEdit == false && list1[3].Fields[0].Mandatory == true && list1[3].Fields[0].Type == FieldTypeEnum.Numeric

);
        }

        [Test]
        public void GetFilteredGroupSmall_4()
        {
            var adfacade = new AdminFacadeSql();
            GroupSmallFilter filt1 = new GroupSmallFilter();
            filt1.Count = 4;
            filt1.OrderEnum = (GroupSmallOrderEnum)0;
            filt1.IsOrderDesc = false;
            filt1.StartIndex = 8;
            filt1.SearchString = "";

            List<GroupSmallModel> list1 = adfacade.GetFilteredGroupSmall(filt1);

            Assert.IsTrue(list1.Count == 4 &&
                list1[0].Id == 1099 && list1[0].Name == "Guests" && list1[0].Count == 0 && list1[0].Level == 1 &&
                    list1[0].Fields[0].FieldId == 1073 && list1[0].Fields[0].Name == "Number_of_visits" && list1[0].Fields[0].UserCanEdit == false && list1[0].Fields[0].Mandatory == true && list1[0].Fields[0].Type == FieldTypeEnum.Numeric &&
                list1[1].Id == 1098 && list1[1].Name == "All users" && list1[1].Count == 0 && list1[1].Level == 0 &&
                    list1[1].Fields[0].FieldId == 1073 && list1[1].Fields[0].Name == "Date_of_last_visit" && list1[1].Fields[0].UserCanEdit == false && list1[1].Fields[0].Mandatory == true && list1[1].Fields[0].Type == FieldTypeEnum.Date &&
                list1[2].Id == 1097 && list1[2].Name == "Admins" && list1[2].Count == 0 && list1[2].Level == 1 &&
                    list1[2].Fields[0].FieldId == 1071 && list1[2].Fields[0].Name == "Number_of_baned_users" && list1[2].Fields[0].UserCanEdit == false && list1[2].Fields[0].Mandatory == false && list1[2].Fields[0].Type == FieldTypeEnum.Numeric &&
                    list1[2].Fields[1].FieldId == 1072 && list1[2].Fields[1].Name == "Ratig" && list1[2].Fields[1].UserCanEdit == false && list1[2].Fields[1].Mandatory == false && list1[2].Fields[1].Type == FieldTypeEnum.Numeric &&
                list1[3].Id == 1096 && list1[3].Name == "Users" && list1[3].Count == 0 && list1[3].Level == 1 &&
                    list1[3].Fields[0].FieldId == 1069 && list1[3].Fields[0].Name == "Date_of_registration" && list1[3].Fields[0].UserCanEdit == false && list1[3].Fields[0].Mandatory == true && list1[3].Fields[0].Type == FieldTypeEnum.Date &&
                    list1[3].Fields[1].FieldId == 1070 && list1[3].Fields[1].Name == "Ratig" && list1[3].Fields[1].UserCanEdit == false && list1[3].Fields[1].Mandatory == false && list1[3].Fields[1].Type == FieldTypeEnum.Numeric
);
        }
        #endregion

        #region GetAvaliableParents
        [Test]
        public void GetAvaliableParents_exists_with_child()
        {
            var adfacade = new AdminFacadeSql();

            Dictionary<int, string> AvalPar = adfacade.GetAvaliableParents(1098);

            Assert.IsTrue(AvalPar.Count == 3 && AvalPar[1096] == "Users" && AvalPar[1100] == "Baned users" && AvalPar[1099] == "Guests");
        }
        [Test]
        public void GetAvaliableParents_exists_with_par()
        {
            var adfacade = new AdminFacadeSql();

            Dictionary<int, string> AvalPar = adfacade.GetAvaliableParents(1097);

            Assert.IsTrue(AvalPar.Count == 3 && AvalPar[1096] == "Users" && AvalPar[1100] == "Baned users" && AvalPar[1099] == "Guests");
        }

        [Test]
        public void GetAvaliableParents_has_no_parents_no_children()
        {
            var adfacade = new AdminFacadeSql();

            Dictionary<int, string> AvalPar = adfacade.GetAvaliableParents(1100);

            Assert.IsTrue(AvalPar.Count == 4 && AvalPar[1096] == "Users" && AvalPar[1097] == "Admins" && AvalPar[1098] == "www" && AvalPar[1099] == "Guests");
        }

        [Test]
        public void GetAvaliableParents_not_exists()
        {
            var adfacade = new AdminFacadeSql();

            Dictionary<int, string> AvalPar = adfacade.GetAvaliableParents(1200);

            Assert.IsNull(AvalPar);
        }

        #endregion

        #region RemoveGroup

        [Test]
        public void RemoveGroup_is_exist_with_fields()
        {
            int id, id_f1, id_f2;
            using (var context = new tpksEntities())
            {
                Group group5 = new Group();
                group5.Group_Name = "TestGroup";
                group5.Parent_Flag = "0";
                context.Group.Add(group5);

                context.SaveChanges();

                id = group5.Group_ID;

                Group_Field field1 = new Group_Field();
                field1.Field_Name = "Date_of_registration";
                field1.Group_ID = id;
                field1.Field_Type = 6;
                field1.Field_Value = "";
                field1.Mandatory_Flag = "1";
                field1.User_Edit_Flag = "0";
                context.Group_Field.Add(field1);

                Group_Field field2 = new Group_Field();
                field2.Field_Name = "Ratig";
                field2.Group_ID = id;
                field2.Field_Type = 2;
                field2.Field_Value = "";
                field2.Mandatory_Flag = "0";
                field2.User_Edit_Flag = "0";
                context.Group_Field.Add(field2);

                context.SaveChanges();

                id_f1 = field1.Field_ID;
                id_f2 = field2.Field_ID;
            }


            var adfacade = new AdminFacadeSql();
            adfacade.RemoveGroup(id);

            using (var Context = new tpksEntities())
            {
                Group gr1 = Context.Group.Find(id);
                Group_Field f1 = Context.Group_Field.Find(id_f1);
                Group_Field f2 = Context.Group_Field.Find(id_f2);

                Assert.IsNull(gr1);
                Assert.IsNull(f1);
                Assert.IsNull(f2);

            }
        }

        [Test]
        public void RemoveGroup_is_exist_without_fields()
        {
            int id;
            using (var context = new tpksEntities())
            {
                Group group5 = new Group();
                group5.Group_Name = "TestGroup";
                group5.Parent_Flag = "0";
                context.Group.Add(group5);

                context.SaveChanges();

                id = group5.Group_ID;
            }


            var adfacade = new AdminFacadeSql();
            adfacade.RemoveGroup(id);

            using (var Context = new tpksEntities())
            {
                Group gr1 = Context.Group.Find(id);

                Assert.IsNull(gr1);
            }
        }
        [Test]
        public void RemoveGroup_is_not_exist()
        {
            var adfacade = new AdminFacadeSql();
            adfacade.RemoveGroup(2000);
            /*
                        using (var Context = new tpksEntities())
                        {
                            Group gr1 = Context.Group.Find(1060);
                            Assert.IsNull(gr1);

                        }*/
        }

        #endregion

        #region AddGroup

        [Test]
        public void AddGroup_empty_string()
        {
            var adfacade = new AdminFacadeSql();

            int id = adfacade.AddGroup("");

            using (var Context = new tpksEntities())
            {
                Group myGroup = Context.Group.Find(id);
                Assert.IsTrue(myGroup.Group_ID == id && myGroup.Group_Name == "" && myGroup.Group_Field.Count == 0 &&
                    myGroup.User.Count == 0 && myGroup.Parent_Flag == "0");
            }
            adfacade.RemoveGroup(id);
        }

        [Test]
        public void AddGroup_string()
        {
            var adfacade = new AdminFacadeSql();

            int id = adfacade.AddGroup("TestGroup");

            using (var Context = new tpksEntities())
            {
                Group myGroup = Context.Group.Find(id);
                Assert.IsTrue(myGroup.Group_ID == id && myGroup.Group_Name == "TestGroup" && myGroup.Group_Field.Count == 0 &&
                    myGroup.User.Count == 0 && myGroup.Parent_Flag == "0");
            }
            adfacade.RemoveGroup(id);
        }

        #endregion

        #region DeleteParentForGroup

        [Test]
        public void DeleteParentForGroup_parent_is_exists_group_is_exists()
        {
            using (var context = new tpksEntities())
            {
                Group_Relation rel4 = new Group_Relation();
                rel4.Child_Group_ID = 1100;
                rel4.Parent_Group_ID = 1096;
                context.Group_Relation.Add(rel4);

                context.SaveChanges();

                var adfacade = new AdminFacadeSql();
                adfacade.DeleteParentForGroup(1096, 1100);

                var allRelations = context.Group_Relation.ToList();
                Group_Relation rel1 = new Group_Relation();
                rel1.Child_Group_ID = 1100;
                rel1.Parent_Group_ID = 1096;

                Assert.IsFalse(allRelations.Contains(rel1));
            }
        }
        [Test]
        public void DeleteParentForGroup_parent_is_not_exists_group_is_exists()
        {
            var adfacade = new AdminFacadeSql();
            adfacade.DeleteParentForGroup(1020, 1009);
        }
        [Test]
        public void DeleteParentForGroup_parent_is_exists_group_is_not_exists()
        {
            var adfacade = new AdminFacadeSql();
            adfacade.DeleteParentForGroup(1011, 1055);
        }
        [Test]
        public void DeleteParentForGroup_parent_is_not_exists_group_is_not_exists()
        {
            var adfacade = new AdminFacadeSql();
            adfacade.DeleteParentForGroup(1000, 1055);
        }



        #endregion]

        #region GetGroupsByUser

        [Test]
        public void GetGroupsByUser_user_with_groups()
        {
            var adfacade = new core.AdminFacadeSql();
            Dictionary<int, string> dict1 = new Dictionary<int, string>();
            Dictionary<int, string> dict2 = new Dictionary<int, string>();
            dict1.Add(1096, "Users");
            using (var context = new tpksEntities()) ;
            {
                dict2 = adfacade.GetGroupsByUser(1048);
                Assert.IsTrue(dict1.SequenceEqual(dict2));
            }
        }

        [Test]
        public void GetGroupsByUser_user_without_groups()
        {
            var adfacade = new core.AdminFacadeSql();
            Dictionary<int, string> dict1 = new Dictionary<int, string>();
            Dictionary<int, string> dict2 = new Dictionary<int, string>();
            using (var context = new tpksEntities()) ;
            {
                dict2 = adfacade.GetGroupsByUser(1049);
                Assert.IsTrue(dict1.SequenceEqual(dict2));
            }
        }

        #endregion

        #region UpdateUser

        [Test]
        public void UpdateUser_exist()
        {
            var adfacade = new core.AdminFacadeSql();

            UserModel user1 = new UserModel();
            user1.Id = 1051;
            user1.UserName = "NewUser";
            user1.FirstName = "NewName";
            user1.SecondName = "NewSecName";
            user1.Surname = "NewLastName";
            user1.UserBd = "2006-06-06";
            user1.UserEmail = "NewMail";
            user1.Phone = "12341234";
            List<GroupModel> groupList1 = new List<GroupModel>();
            GroupModel group1 = new GroupModel();
            group1 = adfacade.GetGroupById(1096);
            groupList1.Add(group1);
            user1.UserGroups = groupList1;
            adfacade.UpdateUser(user1);
            using (var context = new tpksEntities())
            {
                User user2 = context.User.Find(1051);
                Group group2 = context.Group.Find(1096);
                Assert.IsTrue(user2.Login == "NewUser" && user2.User_ID == 1051 && user2.First_Name == "NewName" &&
                    user2.Second_Name == "NewSecName" && user2.Last_Name == "NewLastName" && user2.Birth_Date.Day == 6 &&
                    user2.Birth_Date.Month == 6 && user2.Birth_Date.Year == 2006 && user2.Group.Count == 1 && user2.ICQ == 12341234 &&
                    user2.Group.Contains(group2));
            }
        }
        [Test]
        public void UpdateUser_without_groups()
        {
            var adfacade = new core.AdminFacadeSql();
            UserModel user1 = new UserModel();
            user1.Id = 1051;
            user1.UserName = "NewUser";
            user1.FirstName = "NewName";
            user1.SecondName = "NewSecName";
            user1.Surname = "NewLastName";
            user1.UserBd = "06.06.2006";
            user1.UserEmail = "NewMail";
            user1.Phone = "495";
            adfacade.UpdateUser(user1);
            using (var context = new tpksEntities())
            {
                User user2 = context.User.Find(1051);
                Assert.IsTrue(user2.Login == "NewUser" && user2.User_ID == 1051 && user2.First_Name == "NewName" &&
                    user2.Second_Name == "NewSecName" && user2.Last_Name == "NewLastName" && user2.Birth_Date.Day == 6 &&
                    user2.Birth_Date.Month == 6 && user2.Birth_Date.Year == 2006 && user2.Group.Count == 0 && user2.ICQ == 495);
            }
        }

        [Test]
        public void UpdateUser_with_two_groups()
        {
            var adfacade = new core.AdminFacadeSql();
            UserModel user1 = new UserModel();
            user1.Id = 1051;
            user1.UserName = "NewUser";
            user1.FirstName = "NewName";
            user1.SecondName = "NewSecName";
            user1.Surname = "NewLastName";
            user1.UserBd = "06.06.2006";
            user1.UserEmail = "NewMail";
            user1.Phone = "495";
            List<GroupModel> groupList1 = new List<GroupModel>();
            GroupModel group1 = new GroupModel();
            GroupModel group2 = new GroupModel();
            group1 = adfacade.GetGroupById(1062);
            group2 = adfacade.GetGroupById(1060);
            groupList1.Add(group1);
            user1.UserGroups = groupList1;
            adfacade.UpdateUser(user1);
            using (var context = new tpksEntities())
            {
                User user2 = context.User.Find(1051);
                Group group3 = context.Group.Find(1062);
                Group group4 = context.Group.Find(1060);
                Assert.IsTrue(user2.Login == "NewUser" && user2.User_ID == 1051 && user2.First_Name == "NewName" &&
                    user2.Second_Name == "NewSecName" && user2.Last_Name == "NewLastName" && user2.Birth_Date.Day == 6 &&
                    user2.Birth_Date.Month == 6 && user2.Birth_Date.Year == 2006 && user2.Group.Count == 1 &&
                    user2.Group.Contains(group3) && user2.Group.Contains(group4) && user2.ICQ == 495);
            }
        }

        #endregion

        #region AddParentForGroup
        [Test]
        public void AddParentForGroup_parent_is_not_exists_group_is_exists()
        {
            var adfacade = new AdminFacadeSql();
            adfacade.DeleteParentForGroup(1011, 1009);
            adfacade.AddParentForGroup(1011, 1009);
            using (var context = new tpksEntities())
            {
                var allRelations = context.Group_Relation.ToList();
                Group_Relation rel1 = new Group_Relation();
                rel1.Child_Group_ID = 1009;
                rel1.Parent_Group_ID = 1011;

                Assert.IsTrue(allRelations[0].Child_Group_ID == rel1.Child_Group_ID && allRelations[0].Parent_Group_ID == rel1.Parent_Group_ID);
            }
        }
        #endregion

        #region UpdateGroup
        [Test]
        public void UpdateGroup_updates_group()
        {
            var adfacade = new core.AdminFacadeSql();
            using (var context = new tpksEntities())
            {
                GroupModel group1 = adfacade.GetGroupById(1098);
                group1.Name = "workers";
                adfacade.UpdateGroup(group1);
                GroupModel group2 = adfacade.GetGroupById(1098);
                Assert.IsTrue(group2.Name == group1.Name && group2.Id == group1.Id && group2.Fields.Count == group1.Fields.Count && group2.SelectedParents.Count == group1.SelectedParents.Count);
            }
        }

        #endregion

        #region GetUserWithGroupsList

        [Test]
        public void GetUserWithGroupsList_without_groups()
        {
            var adfacade = new core.AdminFacadeSql();
            UserModel userModel1 = new UserModel();
            userModel1.FirstName = "TestFN";
            userModel1.SecondName = "TestSN";
            userModel1.Surname = "TestSur";
            userModel1.UserName = "TestLog";
            userModel1.Phone = "88888";
            userModel1.UserEmail = "TestMail";
            userModel1.UserBd = "01.01.1991";

            int id1 = adfacade.AddUser(userModel1);

            using (var context = new tpksEntities())
            {
                UserModel user1 = adfacade.GetUserWithGroupsList(id1);
                Assert.IsTrue(user1.Id == id1 && user1.FirstName == "TestFN" && user1.SecondName == "TestSN" && user1.Surname == "TestSur" && user1.UserName == "TestLog" && user1.Phone == null && user1.UserGroups.Count == 0);
            }

            adfacade.RemoveUser(id1);
        }

        [Test]
        public void GetUserWithGroupsList_with_groups()
        {
            var adfacade = new core.AdminFacadeSql();
            UserModel userModel1 = new UserModel();
            userModel1.FirstName = "TestFN";
            userModel1.SecondName = "TestSN";
            userModel1.Surname = "TestSur";
            userModel1.UserName = "TestLog";
            userModel1.Phone = "88888";
            userModel1.UserEmail = "sleeetov@mail.ru";
            userModel1.UserBd = "01.01.1991";

            int id1 = adfacade.AddUser(userModel1);
            int id_g1 = adfacade.AddGroup("test1");
            int id_g2 = adfacade.AddGroup("test2");
            adfacade.AddGroupToUser(id1,id_g1);
            adfacade.AddGroupToUser(id1,id_g2);
            using (var context = new tpksEntities())
            {
                UserModel user1 = adfacade.GetUserWithGroupsList(id1);
                User user2 = context.User.Find(id1);
                Assert.IsTrue(user1.Id == id1 && user1.FirstName == "TestFN" 
                    && user1.SecondName == "TestSN" && user1.Surname == "TestSur" 
                    && user1.UserName == "TestLog" && user1.Phone == null 
                    && user1.UserGroups.Count == 2 && user1.UserGroups[0].Name == "test2" 
                    && user1.UserGroups[1].Name == "test2" && user1.UserGroups[0].Id == id_g2 
                    && user1.UserGroups[1].Id == id_g2);
            }
            adfacade.RemoveGroupFromUser(id1,id_g1);
            adfacade.RemoveGroupFromUser(id1, id_g2);
            adfacade.RemoveUser(id1);
            adfacade.RemoveGroup(id_g1);
            adfacade.RemoveGroup(id_g2);
        }

        [Test]
        public void GetUserWithGroupsList_not_exists()
        {
            var adfacade = new core.AdminFacadeSql();
            using (var context = new tpksEntities())
            {
                UserModel user1 = adfacade.GetUserWithGroupsList(999);
                Assert.IsTrue(user1 == null);
            }
        }

        #endregion

        #region GetAvaliableGroupsForUser

        [Test]
        public void GetAvaliableGroupsForUser_with_2_groups()
        {
            var adfacade = new core.AdminFacadeSql();
            UserModel userModel1 = new UserModel();
            userModel1.FirstName = "TestFN";
            userModel1.SecondName = "TestSN";
            userModel1.Surname = "TestSur";
            userModel1.UserName = "TestLog";
            userModel1.Phone = "88888";
            userModel1.UserEmail = "TestMail";
            userModel1.UserBd = "01.01.1991";

            int id1 = adfacade.AddUser(userModel1);
            int id_g1 = adfacade.AddGroup("test1");
            int id_g2 = adfacade.AddGroup("test2");
            adfacade.AddGroupToUser(id1, id_g1);
            adfacade.AddGroupToUser(id1, id_g2);
            using (var context = new tpksEntities())
            {
                Dictionary<int, string> dic1 = adfacade.GetAvaliableGroupsForUser(id1);
                Dictionary<int, string> dic2 = new Dictionary<int, string>();
                dic2.Add(1096, "Users");
                dic2.Add(1097, "Admins");
                dic2.Add(1098, "workers");
                dic2.Add(1099, "Guests");
                dic2.Add(1100, "Baned users");
                dic2.Add(1140, "GrTestSpecify-Secret-Individual");
                dic2.Add(1141, "GrTestSpecify-Secret-Individual");
                Assert.IsTrue(dic1.SequenceEqual(dic2));
            }
            adfacade.RemoveUser(id1);
            adfacade.RemoveGroup(id_g1);
            adfacade.RemoveGroup(id_g2);
        }

         [Test]
        public void GetAvaliableGroupsForUser_without_groups()
        {
            var adfacade = new core.AdminFacadeSql();
            UserModel userModel1 = new UserModel();
            userModel1.FirstName = "TestFN";
            userModel1.SecondName = "TestSN";
            userModel1.Surname = "TestSur";
            userModel1.UserName = "TestLog";
            userModel1.Phone = "88888";
            userModel1.UserEmail = "TestMail";
            userModel1.UserBd = "01.01.1991";

            int id1 = adfacade.AddUser(userModel1);
            using (var context = new tpksEntities())
            {
                Dictionary <int, string> dic1 = adfacade.GetAvaliableGroupsForUser(id1);
                Dictionary <int, string> dic2 = new Dictionary<int, string>();
                dic2.Add(1096,"Users");
                dic2.Add(1097, "Admins");
                dic2.Add(1098, "workers");
                dic2.Add(1099, "Guests");
                dic2.Add(1100, "Baned users");
                dic2.Add(1140, "GrTestSpecify-Secret-Individual");
                dic2.Add(1141, "GrTestSpecify-Secret-Individual");
                Assert.IsTrue(dic1.SequenceEqual(dic2));
            }

            adfacade.RemoveUser(id1);
        }

         [Test]
         public void GetAvaliableGroupsForUser_with_all_groups()
         {
             var adfacade = new core.AdminFacadeSql();
             UserModel userModel1 = new UserModel();
             userModel1.FirstName = "TestFN";
             userModel1.SecondName = "TestSN";
             userModel1.Surname = "TestSur";
             userModel1.UserName = "TestLog";
             userModel1.Phone = "88888";
             userModel1.UserEmail = "TestMail";
             userModel1.UserBd = "01.01.1991";

             int id1 = adfacade.AddUser(userModel1);
             adfacade.AddGroupToUser(id1, 1096);
             adfacade.AddGroupToUser(id1, 1097);
             adfacade.AddGroupToUser(id1, 1098);
             adfacade.AddGroupToUser(id1, 1099);
             adfacade.AddGroupToUser(id1, 1100);
             adfacade.AddGroupToUser(id1, 1140);
             adfacade.AddGroupToUser(id1, 1141);
             using (var context = new tpksEntities())
             {
                 Dictionary<int, string> dic1 = adfacade.GetAvaliableGroupsForUser(id1);
                 Assert.IsTrue(dic1.Count==0);
             }
             adfacade.RemoveUser(id1);
         }

         [Test]
         public void GetAvaliableGroupsForUser_user_not_exist()
         {
             var adfacade = new core.AdminFacadeSql();
             using (var context = new tpksEntities())
             {
                 Dictionary<int, string> dic1 = adfacade.GetAvaliableGroupsForUser(1199);
                 Assert.IsTrue(dic1.Count == 0);
             }
         }

        #endregion

        #endregion

        #region Routes
        
        #region CreateRoute

        [Test]
        public void CreateRoute_get_not_filled_model()
         {
             var adfacade = new AdminFacadeSql();

             RouteModel routeTest = new RouteModel();

             Assert.IsTrue(adfacade.CreateRoute(routeTest) == 1);
         }

        [Test]
        public void CreateRoute_full1()
        {
            var adfacade = new AdminFacadeSql();

            RouteModel routeTest = new RouteModel();
            routeTest.RouteName = "TestRouteFullForCrea";
            
            routeTest.WayParams = new List<Way_Prm>();

            Way_Prm wayPrmTest1 = new Way_Prm();
            wayPrmTest1.Prm_Name = "time";
            wayPrmTest1.Prm_Value = "20";
            routeTest.WayParams.Add(wayPrmTest1);

            PointModel pointTest = new PointModel();

            Step stepTest = new Step();
            stepTest.Step_Name = "FirstStepFullForCrea";

            pointTest.Step = stepTest;

            Step_Prm stepPrmTest = new Step_Prm();
            stepPrmTest.Prm_Name = "TimeTestFullForCrea";
            stepPrmTest.Prm_Value = "20";

            pointTest.Params.Add(stepPrmTest);

            Step_Field stepFieldTest = new Step_Field();
            stepFieldTest.Field_Name = "TestFieldFullForCrea";
            stepFieldTest.Field_Type = 1;
            stepFieldTest.Field_Value = "TestValueFullForCrea";
            stepFieldTest.Visible_Flag = "1";
            stepFieldTest.Editable_Falg = "1";

            pointTest.Fields.Add(stepFieldTest);

            routeTest.Points.Add(pointTest);

            PointModel pointTest1 = new PointModel();

            Step stepTest1 = new Step();
            stepTest1.Step_Name = "FirstStepFullForCrea1";
            //stepTest.Step_Nmbr = 1;

            pointTest1.Step = stepTest1;

            Step_Prm stepPrmTest1 = new Step_Prm();
            stepPrmTest1.Prm_Name = "TimeTestFullForCrea1";
            stepPrmTest1.Prm_Value = "20";

            pointTest1.Params.Add(stepPrmTest1);

            Step_Field stepFieldTest1 = new Step_Field();
            stepFieldTest1.Field_Name = "TestFieldFullForCrea1";
            stepFieldTest1.Field_Type = 1;
            stepFieldTest1.Field_Value = "TestValueFullForCrea1";
            stepFieldTest1.Visible_Flag = "1";
            stepFieldTest1.Editable_Falg = "1";

            pointTest1.Fields.Add(stepFieldTest1);

            routeTest.Points.Add(pointTest1);


            using (var context = new tpksEntities())
            {
                for (int i = 1144; i < 1200; i++)
                {
                    Way forDel = context.Way.Find(i);
                    if (forDel!=null)
                    {
                        if (forDel.Way_Name == "TestRouteFullForCrea")
                        {
                            adfacade.DeleteRoute(i);
                        }
                    }
                }

                int res = adfacade.CreateRoute(routeTest);

                var allWays = context.Way.ToList();

                Way dbWayTest =
                    context.Way.Include("Way_Prm").Include("Step").FirstOrDefault(x => x.Way_Name == "TestRouteFullForCrea");
                                
                Assert.IsTrue(res == 0 && dbWayTest.Way_Name == "TestRouteFullForCrea" 
                    && dbWayTest.Way_Prm.Count == 1 && dbWayTest.Step.Count ==2);

                var dbParams = dbWayTest.Way_Prm.ToList();

                Assert.IsTrue(dbParams[0].Prm_Name == "time" && dbParams[0].Prm_Value == "20");

                var dbSteps = dbWayTest.Step.ToList();

                Assert.IsTrue(dbSteps[0].Step_Name == "FirstStepFullForCrea");

                var dbFields1 = dbSteps[0].Step_Field.ToList();
                var dbStPrm1 = dbSteps[0].Step_Prm.ToList();
                Assert.IsTrue(dbFields1[0].Field_Name == "TestFieldFullForCrea" && dbFields1[0].Field_Type == 1
                && dbFields1[0].Editable_Falg == "1"
                    && dbFields1[0].Visible_Flag == "1" &&

                    dbStPrm1[0].Prm_Name == "TimeTestFullForCrea" && dbStPrm1[0].Prm_Value == "20");

                var dbFields2 = dbSteps[1].Step_Field.ToList();
                var dbStPrm2 = dbSteps[1].Step_Prm.ToList();
                Assert.IsTrue(dbFields2[0].Field_Name == "TestFieldFullForCrea1" && dbFields2[0].Field_Type == 1
                && dbFields2[0].Editable_Falg == "1"
                    && dbFields2[0].Visible_Flag == "1" &&

                    dbStPrm2[0].Prm_Name == "TimeTestFullForCrea1" && dbStPrm2[0].Prm_Value == "20");

                adfacade.DeleteRoute(dbWayTest.Way_ID);
            }
        }

        [Test]
        public void CreateRoute_full2()
        {
            var adfacade = new AdminFacadeSql();

            RouteModel routeTest = new RouteModel();
            routeTest.RouteName = "TestRouteFullForCrea";

            routeTest.WayParams = new List<Way_Prm>();

            Way_Prm wayPrmTest1 = new Way_Prm();
            wayPrmTest1.Prm_Name = "time";
            wayPrmTest1.Prm_Value = "20";
            routeTest.WayParams.Add(wayPrmTest1);

            PointModel pointTest = new PointModel();

            Step stepTest = new Step();
            stepTest.Step_Name = "FirstStepFullForCrea";

            pointTest.Step = stepTest;

            Step_Prm stepPrmTest = new Step_Prm();
            stepPrmTest.Prm_Name = "TimeTestFullForCrea";
            stepPrmTest.Prm_Value = "20";

            pointTest.Params.Add(stepPrmTest);

            Step_Field stepFieldTest = new Step_Field();
            stepFieldTest.Field_Name = "TestFieldFullForCrea";
            stepFieldTest.Field_Type = 1;
            stepFieldTest.Field_Value = "TestValueFullForCrea";
            stepFieldTest.Visible_Flag = "1";
            stepFieldTest.Editable_Falg = "1";

            pointTest.Fields.Add(stepFieldTest);

            Step_Field stepFieldTest1 = new Step_Field();
            stepFieldTest1.Field_Name = "TestFieldFullForCrea1";
            stepFieldTest1.Field_Type = 1;
            stepFieldTest1.Field_Value = "TestValueFullForCrea1";
            stepFieldTest1.Visible_Flag = "1";
            stepFieldTest1.Editable_Falg = "1";

            pointTest.Fields.Add(stepFieldTest1);

            routeTest.Points.Add(pointTest);


            using (var context = new tpksEntities())
            {
                for (int i = 1144; i < 1200; i++)
                {
                    Way forDel = context.Way.Find(i);
                    if (forDel != null)
                    {
                        if (forDel.Way_Name == "TestRouteFullForCrea")
                        {
                            adfacade.DeleteRoute(i);
                        }
                    }
                }

                int res = adfacade.CreateRoute(routeTest);

                var allWays = context.Way.ToList();

                Way dbWayTest =
                    context.Way.Include("Way_Prm").Include("Step").FirstOrDefault(x => x.Way_Name == "TestRouteFullForCrea");

                Assert.IsTrue(res == 0 && dbWayTest.Way_Name == "TestRouteFullForCrea"
                    && dbWayTest.Way_Prm.Count == 1 && dbWayTest.Step.Count == 1);

                var dbParams = dbWayTest.Way_Prm.ToList();

                Assert.IsTrue(dbParams[0].Prm_Name == "time" && dbParams[0].Prm_Value == "20");

                var dbSteps = dbWayTest.Step.ToList();

                Assert.IsTrue(dbSteps[0].Step_Name == "FirstStepFullForCrea");

                var dbFields1 = dbSteps[0].Step_Field.ToList();

                var dbStPrm1 = dbSteps[0].Step_Prm.ToList();
                Assert.IsTrue(dbFields1[0].Field_Name == "TestFieldFullForCrea" && dbFields1[0].Field_Type == 1
                && dbFields1[0].Editable_Falg == "1"
                    && dbFields1[0].Visible_Flag == "1" &&

                    dbStPrm1[0].Prm_Name == "TimeTestFullForCrea" && dbStPrm1[0].Prm_Value == "20");

                Assert.IsTrue(dbFields1[1].Field_Name == "TestFieldFullForCrea1" && dbFields1[1].Field_Type == 1
                && dbFields1[1].Editable_Falg == "1"
                    && dbFields1[1].Visible_Flag == "1");

                adfacade.DeleteRoute(dbWayTest.Way_ID);
            }
        }

        [Test]
        public void CreateRoute_full4()
        {
            var adfacade = new AdminFacadeSql();

            RouteModel routeTest = new RouteModel();
            routeTest.RouteName = "TestRouteFullForCrea";

            routeTest.WayParams = new List<Way_Prm>();

            Way_Prm wayPrmTest1 = new Way_Prm();
            wayPrmTest1.Prm_Name = "time";
            wayPrmTest1.Prm_Value = "20";
            routeTest.WayParams.Add(wayPrmTest1);

            using (var context = new tpksEntities())
            {
                for (int i = 1144; i < 1200; i++)
                {
                    Way forDel = context.Way.Find(i);
                    if (forDel != null)
                    {
                        if (forDel.Way_Name == "TestRouteFullForCrea")
                        {
                            adfacade.DeleteRoute(i);
                        }
                    }
                }

                int res = adfacade.CreateRoute(routeTest);

                var allWays = context.Way.ToList();

                Way dbWayTest =
                    context.Way.Include("Way_Prm").Include("Step").FirstOrDefault(x => x.Way_Name == "TestRouteFullForCrea");

                Assert.IsTrue(res == 0 && dbWayTest.Way_Name == "TestRouteFullForCrea"
                    && dbWayTest.Way_Prm.Count == 1 && dbWayTest.Step.Count == 0);

                var dbParams = dbWayTest.Way_Prm.ToList();

                Assert.IsTrue(dbParams[0].Prm_Name == "time" && dbParams[0].Prm_Value == "20");

                adfacade.DeleteRoute(dbWayTest.Way_ID);
            }
        }

        [Test]
        public void CreateRoute_full5()
        {
            var adfacade = new AdminFacadeSql();

            RouteModel routeTest = new RouteModel();
            routeTest.RouteName = "TestRouteFullForCrea";

            routeTest.WayParams = new List<Way_Prm>();

            Way_Prm wayPrmTest1 = new Way_Prm();
            wayPrmTest1.Prm_Name = "time";
            wayPrmTest1.Prm_Value = "20";
            routeTest.WayParams.Add(wayPrmTest1);

            PointModel pointTest = new PointModel();

            Step stepTest = new Step();
            stepTest.Step_Name = "FirstStepFullForCrea";

            pointTest.Step = stepTest;

            Step_Prm stepPrmTest = new Step_Prm();
            stepPrmTest.Prm_Name = "TimeTestFullForCrea";
            stepPrmTest.Prm_Value = "20";

            pointTest.Params.Add(stepPrmTest);

            routeTest.Points.Add(pointTest);

            PointModel pointTest1 = new PointModel();

            Step stepTest1 = new Step();
            stepTest1.Step_Name = "FirstStepFullForCrea1";
            //stepTest.Step_Nmbr = 1;

            pointTest1.Step = stepTest1;

            Step_Prm stepPrmTest1 = new Step_Prm();
            stepPrmTest1.Prm_Name = "TimeTestFullForCrea1";
            stepPrmTest1.Prm_Value = "20";

            pointTest1.Params.Add(stepPrmTest1);

            routeTest.Points.Add(pointTest1);


            using (var context = new tpksEntities())
            {
                for (int i = 1144; i < 1200; i++)
                {
                    Way forDel = context.Way.Find(i);
                    if (forDel != null)
                    {
                        if (forDel.Way_Name == "TestRouteFullForCrea")
                        {
                            adfacade.DeleteRoute(i);
                        }
                    }
                }

                int res = adfacade.CreateRoute(routeTest);

                var allWays = context.Way.ToList();

                Way dbWayTest =
                    context.Way.Include("Way_Prm").Include("Step").FirstOrDefault(x => x.Way_Name == "TestRouteFullForCrea");

                Assert.IsTrue(res == 0 && dbWayTest.Way_Name == "TestRouteFullForCrea"
                    && dbWayTest.Way_Prm.Count == 1 && dbWayTest.Step.Count == 2);

                var dbParams = dbWayTest.Way_Prm.ToList();

                Assert.IsTrue(dbParams[0].Prm_Name == "time" && dbParams[0].Prm_Value == "20");

                var dbSteps = dbWayTest.Step.ToList();

                Assert.IsTrue(dbSteps[0].Step_Name == "FirstStepFullForCrea");

                var dbFields1 = dbSteps[0].Step_Field.ToList();
                var dbStPrm1 = dbSteps[0].Step_Prm.ToList();

                Assert.IsTrue(dbFields1.Count == 0 &&
                    dbStPrm1[0].Prm_Name == "TimeTestFullForCrea" && dbStPrm1[0].Prm_Value == "20");

                var dbFields2 = dbSteps[1].Step_Field.ToList();
                var dbStPrm2 = dbSteps[1].Step_Prm.ToList();
                Assert.IsTrue(dbFields2.Count == 0 && 
                    dbStPrm2[0].Prm_Name == "TimeTestFullForCrea1" && dbStPrm2[0].Prm_Value == "20");

                adfacade.DeleteRoute(dbWayTest.Way_ID);
            }
        }


        #endregion

        #region DeleteRoute

        [Test]
        public void DeleteRoute_not_exist()
        {
            var adfacade = new AdminFacadeSql();

            Assert.IsTrue(adfacade.DeleteRoute(999) == 1);

        }

        [Test]
        public void DeleteRoute_without_steps_without_prm()
        {
            using (var context = new tpksEntities())
            {
                var allWaysBefore = context.Way.ToList();
                var allways_paramBefore = context.Way_Prm.ToList();
                var allStepsBefore = context.Step.ToList();
                var allSteps_prmBefore = context.Step_Prm.ToList();
                var allStep_filedsBefore = context.Step_Field.ToList();

                Way wayTest = new Way();
                wayTest.Way_Name = "testWayNotFilled";
                wayTest.Creation_Date = DateTime.Now;
                context.Way.Add(wayTest);
                context.SaveChanges();

                int id = wayTest.Way_ID;

                var adfacade = new AdminFacadeSql();

                int res = adfacade.DeleteRoute(id);
                RouteModel route1 = adfacade.GetRouteById(id);

                var allWays = context.Way.ToList();
                var allways_param = context.Way_Prm.ToList();
                var allSteps = context.Step.ToList();
                var allSteps_prm = context.Step_Prm.ToList();
                var allStep_fileds = context.Step_Field.ToList();

                Assert.IsTrue(res == 0 && route1.RouteId == -2 && allWaysBefore.Count == allWays.Count &&
                    allways_paramBefore.Count == allways_param.Count && allStepsBefore.Count == allSteps.Count &&
                    allStep_filedsBefore.Count == allStep_fileds.Count && allSteps_prmBefore.Count == allSteps_prm.Count);
            }
        }

        [Test]
        public void DeleteRoute_with_steps_without_prm()
        {
            using (var context = new tpksEntities())
            {
                var allWaysBefore = context.Way.ToList();
                var allways_paramBefore = context.Way_Prm.ToList();
                var allStepsBefore = context.Step.ToList();
                var allSteps_prmBefore = context.Step_Prm.ToList();
                var allStep_filedsBefore = context.Step_Field.ToList();

                Way wayTest = new Way();
                wayTest.Way_Name = "testWay";
                wayTest.Creation_Date = DateTime.Now;
                context.Way.Add(wayTest);
                context.SaveChanges();
                
                int idWay = wayTest.Way_ID;

                Way_Prm wayPrmTest = new Way_Prm();
                wayPrmTest.Way_ID = idWay;
                wayPrmTest.Prm_Name = "TimeTest";
                wayPrmTest.Prm_Value = "20";
                wayPrmTest.Field_Type = 1;
                context.Way_Prm.Add(wayPrmTest);
                context.SaveChanges();

                int idWayPrm = wayPrmTest.Cntr;

                Step stepTest = new Step();
                stepTest.Step_Name = "First step";
                stepTest.Way_ID = idWay;
                stepTest.Step_Nmbr = 1;
                context.Step.Add(stepTest);
                context.SaveChanges();

                int idStep = stepTest.Step_ID;
                
                var adfacade = new AdminFacadeSql();

                int res = adfacade.DeleteRoute(idWay);

                RouteModel route1 = adfacade.GetRouteById(idWay);

                var allWays = context.Way.ToList();
                var allways_param = context.Way_Prm.ToList();
                var allSteps = context.Step.ToList();
                var allSteps_prm = context.Step_Prm.ToList();
                var allStep_fileds = context.Step_Field.ToList();

                Assert.IsTrue(res == 0 && route1.RouteId == -2 && allWaysBefore.Count == allWays.Count &&
                    allways_paramBefore.Count == allways_param.Count && allStepsBefore.Count == allSteps.Count &&
                    allStep_filedsBefore.Count == allStep_fileds.Count && allSteps_prmBefore.Count == allSteps_prm.Count);
            }

        }

        [Test]
        public void DeleteRoute_without_steps_with_prm()
        {
            using (var context = new tpksEntities())
            {
                var allWaysBefore = context.Way.ToList();
                var allways_paramBefore = context.Way_Prm.ToList();
                var allStepsBefore = context.Step.ToList();
                var allSteps_prmBefore = context.Step_Prm.ToList();
                var allStep_filedsBefore = context.Step_Field.ToList();
                
                Way wayTest = new Way();
                wayTest.Way_Name = "testWay";
                wayTest.Creation_Date = DateTime.Now;
                context.Way.Add(wayTest);
                context.SaveChanges();

                int idWay = wayTest.Way_ID;


                Way_Prm wayPrmTest = new Way_Prm();
                wayPrmTest.Way_ID = idWay;
                wayPrmTest.Prm_Name = "TimeTest";
                wayPrmTest.Prm_Value = "20";
                wayPrmTest.Field_Type = 1;
                context.Way_Prm.Add(wayPrmTest);
                context.SaveChanges();

                int idWayPrm = wayPrmTest.Cntr;

                var adfacade = new AdminFacadeSql();

                int res = adfacade.DeleteRoute(idWay);

                RouteModel route1 = adfacade.GetRouteById(idWay);

                var allWays = context.Way.ToList();
                var allways_param = context.Way_Prm.ToList();
                var allSteps = context.Step.ToList();
                var allSteps_prm = context.Step_Prm.ToList();
                var allStep_fileds = context.Step_Field.ToList();

                Assert.IsTrue(res == 0 && route1.RouteId == -2 && allWaysBefore.Count == allWays.Count &&
                    allways_paramBefore.Count == allways_param.Count && allStepsBefore.Count == allSteps.Count &&
                    allStep_filedsBefore.Count == allStep_fileds.Count && allSteps_prmBefore.Count == allSteps_prm.Count);
            }

        }

        [Test]
        public void DeleteRoute_with_steps_with_prm_with_fields()
        {
            using (var context = new tpksEntities())
            {
                var allWaysBefore = context.Way.ToList();
                var allways_paramBefore = context.Way_Prm.ToList();
                var allStepsBefore = context.Step.ToList();
                var allSteps_prmBefore = context.Step_Prm.ToList();
                var allStep_filedsBefore = context.Step_Field.ToList();

                Way wayTest = new Way();
                wayTest.Way_Name = "testWayFullForDel";
                wayTest.Creation_Date = DateTime.Now;
                context.Way.Add(wayTest);
                context.SaveChanges();

                int idWay = wayTest.Way_ID;

                Way_Prm wayPrmTest = new Way_Prm();
                wayPrmTest.Way_ID = idWay;
                wayPrmTest.Prm_Name = "TimeTestFullForDel";
                wayPrmTest.Prm_Value = "20";
                wayPrmTest.Field_Type = 1;
                context.Way_Prm.Add(wayPrmTest);
                context.SaveChanges();

                int idWayPrm = wayPrmTest.Cntr;

                Step stepTest = new Step();
                stepTest.Step_Name = "FirstStepFullForDel";
                stepTest.Way_ID = idWay;
                stepTest.Step_Nmbr = 1;
                context.Step.Add(stepTest);
                context.SaveChanges();

                int idStep = stepTest.Step_ID;

                Step_Prm stepPrmTest = new Step_Prm();
                stepPrmTest.Prm_Name = "TimeTestFullForDel";
                stepPrmTest.Prm_Value = "20";
                stepPrmTest.Step_ID = idStep;
                context.Step_Prm.Add(stepPrmTest);
                context.SaveChanges();

                int idStepPrm = stepPrmTest.Cntr;

                Step_Field stepFieldTest = new Step_Field();
                stepFieldTest.Step_ID = idStep;
                stepFieldTest.Field_Name = "TestFieldFullForDel";
                stepFieldTest.Field_Type = 1;
                stepFieldTest.Field_Value = "TestValueFullForDel";
                stepFieldTest.Visible_Flag = "1";
                stepFieldTest.Editable_Falg = "1";
                context.Step_Field.Add(stepFieldTest);
                context.SaveChanges();

                int idStepField = stepFieldTest.Cntr;


                var adfacade = new AdminFacadeSql();

                int res = adfacade.DeleteRoute(idWay);

                Way way1 = context.Way.Find(idWay);

                var allWays = context.Way.ToList();
                var allways_param = context.Way_Prm.ToList();
                var allSteps = context.Step.ToList();
                var allSteps_prm = context.Step_Prm.ToList();
                var allStep_fileds = context.Step_Field.ToList();

                RouteModel route1 = adfacade.GetRouteById(idWay);

                Way way2 = context.Way.Find(idWay);

                Assert.IsTrue(res == 0 && route1.RouteId == -2 && allWaysBefore.Count == allWays.Count &&
                    allways_paramBefore.Count == allways_param.Count && allStepsBefore.Count == allSteps.Count &&
                    allStep_filedsBefore.Count == allStep_fileds.Count && allSteps_prmBefore.Count == allSteps_prm.Count);
            }
        }


        #endregion

        #region UpdateRoute

        [Test]
        public void UpdateRoute_get_not_filled_model()
        {
            RouteModel routeTest = new RouteModel();

            var adfacade = new AdminFacadeSql();

            Assert.IsTrue(adfacade.UpdateRoute(routeTest) == 1);
        }

        [Test]
        public void UpdateRoute_id_does_not_exist()
        {

            var adfacade = new AdminFacadeSql();
            RouteModel routeTest = new RouteModel();
            routeTest.RouteName = "TestRouteFullForCrea";
            routeTest.RouteId = 999;

            routeTest.WayParams = new List<Way_Prm>();

            Way_Prm wayPrmTest1 = new Way_Prm();
            wayPrmTest1.Prm_Name = "time";
            wayPrmTest1.Prm_Value = "20";
            routeTest.WayParams.Add(wayPrmTest1);

            PointModel pointTest = new PointModel();

            Step stepTest = new Step();
            stepTest.Step_Name = "FirstStepFullForCrea";

            pointTest.Step = stepTest;

            Step_Prm stepPrmTest = new Step_Prm();
            stepPrmTest.Prm_Name = "TimeTestFullForCrea";
            stepPrmTest.Prm_Value = "20";

            pointTest.Params.Add(stepPrmTest);

            Step_Field stepFieldTest = new Step_Field();
            stepFieldTest.Field_Name = "TestFieldFullForCrea";
            stepFieldTest.Field_Type = 1;
            stepFieldTest.Field_Value = "TestValueFullForCrea";
            stepFieldTest.Visible_Flag = "1";
            stepFieldTest.Editable_Falg = "1";

            pointTest.Fields.Add(stepFieldTest);

            Step_Field stepFieldTest1 = new Step_Field();
            stepFieldTest1.Field_Name = "TestFieldFullForCrea1";
            stepFieldTest1.Field_Type = 1;
            stepFieldTest1.Field_Value = "TestValueFullForCrea1";
            stepFieldTest1.Visible_Flag = "1";
            stepFieldTest1.Editable_Falg = "1";

            pointTest.Fields.Add(stepFieldTest1);

            routeTest.Points.Add(pointTest);


            using (var context = new tpksEntities())
            {
                int res = adfacade.UpdateRoute(routeTest);
                Assert.IsTrue(res == 1);
            }
        }

        [Test]
        public void UpdateRoute_one_step_with_two_fields()
        {
            var adfacade = new AdminFacadeSql();

            RouteModel routeTest = new RouteModel();
            routeTest.RouteName = "TestRouteFullForCrea";
            
            routeTest.WayParams = new List<Way_Prm>();

            Way_Prm wayPrmTest1 = new Way_Prm();
            wayPrmTest1.Prm_Name = "time";
            wayPrmTest1.Prm_Value = "20";
            routeTest.WayParams.Add(wayPrmTest1);

            PointModel pointTest = new PointModel();

            Step stepTest = new Step();
            stepTest.Step_Name = "FirstStepFullForCrea";

            pointTest.Step = stepTest;

            Step_Prm stepPrmTest = new Step_Prm();
            stepPrmTest.Prm_Name = "TimeTestFullForCrea";
            stepPrmTest.Prm_Value = "20";

            pointTest.Params.Add(stepPrmTest);

            Step_Field stepFieldTest = new Step_Field();
            stepFieldTest.Field_Name = "TestFieldFullForCrea";
            stepFieldTest.Field_Type = 1;
            stepFieldTest.Field_Value = "TestValueFullForCrea";
            stepFieldTest.Visible_Flag = "1";
            stepFieldTest.Editable_Falg = "1";

            pointTest.Fields.Add(stepFieldTest);

            routeTest.Points.Add(pointTest);

            PointModel pointTest1 = new PointModel();

            Step stepTest1 = new Step();
            stepTest1.Step_Name = "FirstStepFullForCrea1";
            //stepTest.Step_Nmbr = 1;

            pointTest1.Step = stepTest1;

            Step_Prm stepPrmTest1 = new Step_Prm();
            stepPrmTest1.Prm_Name = "TimeTestFullForCrea1";
            stepPrmTest1.Prm_Value = "20";

            pointTest1.Params.Add(stepPrmTest1);

            Step_Field stepFieldTest1 = new Step_Field();
            stepFieldTest1.Field_Name = "TestFieldFullForCrea1";
            stepFieldTest1.Field_Type = 1;
            stepFieldTest1.Field_Value = "TestValueFullForCrea1";
            stepFieldTest1.Visible_Flag = "1";
            stepFieldTest1.Editable_Falg = "1";

            pointTest1.Fields.Add(stepFieldTest1);

            routeTest.Points.Add(pointTest1);


            using (var context = new tpksEntities())
            {
                for (int i = 1144; i < 1200; i++)
                {
                    Way forDel = context.Way.Find(i);
                    if (forDel != null)
                    {
                        if (forDel.Way_Name == "TestRouteFullForCrea")
                        {
                            adfacade.DeleteRoute(i);
                        }
                    }
                }

                int res = adfacade.CreateRoute(routeTest);

                Way dbWayTest = 
                    context.Way.Include("Way_Prm").Include("Step").FirstOrDefault(x => x.Way_Name == "TestRouteFullForCrea");


            routeTest = new RouteModel();
                routeTest.RouteId = dbWayTest.Way_ID;
            routeTest.RouteName = "TestRouteFullForCrea";

            routeTest.WayParams = new List<Way_Prm>();

            wayPrmTest1 = new Way_Prm();
            wayPrmTest1.Prm_Name = "time";
            wayPrmTest1.Prm_Value = "20";
            routeTest.WayParams.Add(wayPrmTest1);

            pointTest = new PointModel();

            stepTest = new Step();
            stepTest.Step_Name = "FirstStepFullForCrea";

            pointTest.Step = stepTest;

            stepPrmTest = new Step_Prm();
            stepPrmTest.Prm_Name = "TimeTestFullForCrea";
            stepPrmTest.Prm_Value = "20";

            pointTest.Params.Add(stepPrmTest);

            stepFieldTest = new Step_Field();
            stepFieldTest.Field_Name = "TestFieldFullForCrea";
            stepFieldTest.Field_Type = 1;
            stepFieldTest.Field_Value = "TestValueFullForCrea";
            stepFieldTest.Visible_Flag = "1";
            stepFieldTest.Editable_Falg = "1";

            pointTest.Fields.Add(stepFieldTest);

            stepFieldTest1 = new Step_Field();
            stepFieldTest1.Field_Name = "TestFieldFullForCrea1";
            stepFieldTest1.Field_Type = 1;
            stepFieldTest1.Field_Value = "TestValueFullForCrea1";
            stepFieldTest1.Visible_Flag = "1";
            stepFieldTest1.Editable_Falg = "1";

            pointTest.Fields.Add(stepFieldTest1);

            routeTest.Points.Add(pointTest);


                res = adfacade.UpdateRoute(routeTest);

                var allWays = context.Way.ToList();

                dbWayTest =
    context.Way.Include("Way_Prm").Include("Step").FirstOrDefault(x => x.Way_Name == "TestRouteFullForCrea");

                Assert.IsTrue(res == 0 && dbWayTest.Way_Name == "TestRouteFullForCrea"
                    && dbWayTest.Way_Prm.Count == 1 && dbWayTest.Step.Count == 1);

                var dbParams = dbWayTest.Way_Prm.ToList();

                Assert.IsTrue(dbParams[0].Prm_Name == "time" && dbParams[0].Prm_Value == "20");

                var dbSteps = dbWayTest.Step.ToList();

                Assert.IsTrue(dbSteps[0].Step_Name == "FirstStepFullForCrea");

                var dbFields1 = dbSteps[0].Step_Field.ToList();

                var dbStPrm1 = dbSteps[0].Step_Prm.ToList();
                Assert.IsTrue(dbFields1[0].Field_Name == "TestFieldFullForCrea" && dbFields1[0].Field_Type == 1
                && dbFields1[0].Editable_Falg == "1"
                    && dbFields1[0].Visible_Flag == "1" &&

                    dbStPrm1[0].Prm_Name == "TimeTestFullForCrea" && dbStPrm1[0].Prm_Value == "20");

                Assert.IsTrue(dbFields1[1].Field_Name == "TestFieldFullForCrea1" && dbFields1[1].Field_Type == 1
                && dbFields1[1].Editable_Falg == "1"
                    && dbFields1[1].Visible_Flag == "1");

                adfacade.DeleteRoute(dbWayTest.Way_ID);
            }
        }

        [Test]
        public void UpdateRoute_two_steps_with_one_field()
        {
            var adfacade = new AdminFacadeSql();

            RouteModel routeTest = new RouteModel();
            routeTest.RouteName = "TestRouteFullForCrea";

            routeTest.WayParams = new List<Way_Prm>();

            Way_Prm wayPrmTest1 = new Way_Prm();
            wayPrmTest1.Prm_Name = "time";
            wayPrmTest1.Prm_Value = "20";
            routeTest.WayParams.Add(wayPrmTest1);

            PointModel pointTest = new PointModel();

            Step stepTest = new Step();
            stepTest.Step_Name = "FirstStepFullForCrea";

            pointTest.Step = stepTest;

            Step_Prm stepPrmTest = new Step_Prm();
            stepPrmTest.Prm_Name = "TimeTestFullForCrea";
            stepPrmTest.Prm_Value = "20";

            pointTest.Params.Add(stepPrmTest);

            Step_Field stepFieldTest = new Step_Field();
            stepFieldTest.Field_Name = "TestFieldFullForCrea";
            stepFieldTest.Field_Type = 1;
            stepFieldTest.Field_Value = "TestValueFullForCrea";
            stepFieldTest.Visible_Flag = "1";
            stepFieldTest.Editable_Falg = "1";

            pointTest.Fields.Add(stepFieldTest);

            Step_Field stepFieldTest1 = new Step_Field();
            stepFieldTest1.Field_Name = "TestFieldFullForCrea1";
            stepFieldTest1.Field_Type = 1;
            stepFieldTest1.Field_Value = "TestValueFullForCrea1";
            stepFieldTest1.Visible_Flag = "1";
            stepFieldTest1.Editable_Falg = "1";

            pointTest.Fields.Add(stepFieldTest1);

            routeTest.Points.Add(pointTest);


            using (var context = new tpksEntities())
            {
                for (int i = 1144; i < 1200; i++)
                {
                    Way forDel = context.Way.Find(i);
                    if (forDel != null)
                    {
                        if (forDel.Way_Name == "TestRouteFullForCrea")
                        {
                            adfacade.DeleteRoute(i);
                        }
                    }
                }

                int res = adfacade.CreateRoute(routeTest);

                var allWays = context.Way.ToList();

                Way dbWayTest =
                    context.Way.Include("Way_Prm").Include("Step").FirstOrDefault(x => x.Way_Name == "TestRouteFullForCrea");


             routeTest = new RouteModel();
            routeTest.RouteName = "TestRouteFullForCrea";
                routeTest.RouteId = dbWayTest.Way_ID;
            
            routeTest.WayParams = new List<Way_Prm>();

            wayPrmTest1 = new Way_Prm();
            wayPrmTest1.Prm_Name = "time";
            wayPrmTest1.Prm_Value = "20";
            routeTest.WayParams.Add(wayPrmTest1);

            pointTest = new PointModel();

            stepTest = new Step();
            stepTest.Step_Name = "FirstStepFullForCrea";

            pointTest.Step = stepTest;

            stepPrmTest = new Step_Prm();
            stepPrmTest.Prm_Name = "TimeTestFullForCrea";
            stepPrmTest.Prm_Value = "20";

            pointTest.Params.Add(stepPrmTest);

            stepFieldTest = new Step_Field();
            stepFieldTest.Field_Name = "TestFieldFullForCrea";
            stepFieldTest.Field_Type = 1;
            stepFieldTest.Field_Value = "TestValueFullForCrea";
            stepFieldTest.Visible_Flag = "1";
            stepFieldTest.Editable_Falg = "1";

            pointTest.Fields.Add(stepFieldTest);

            routeTest.Points.Add(pointTest);

            PointModel pointTest1 = new PointModel();

            Step stepTest1 = new Step();
            stepTest1.Step_Name = "FirstStepFullForCrea1";
            //stepTest.Step_Nmbr = 1;

            pointTest1.Step = stepTest1;

            Step_Prm stepPrmTest1 = new Step_Prm();
            stepPrmTest1.Prm_Name = "TimeTestFullForCrea1";
            stepPrmTest1.Prm_Value = "20";

            pointTest1.Params.Add(stepPrmTest1);

            stepFieldTest1 = new Step_Field();
            stepFieldTest1.Field_Name = "TestFieldFullForCrea1";
            stepFieldTest1.Field_Type = 1;
            stepFieldTest1.Field_Value = "TestValueFullForCrea1";
            stepFieldTest1.Visible_Flag = "1";
            stepFieldTest1.Editable_Falg = "1";

            pointTest1.Fields.Add(stepFieldTest1);

            routeTest.Points.Add(pointTest1);


                res = adfacade.CreateRoute(routeTest);

                dbWayTest =
                    context.Way.Include("Way_Prm").Include("Step").FirstOrDefault(x => x.Way_Name == "TestRouteFullForCrea");
                                
                Assert.IsTrue(res == 0 && dbWayTest.Way_Name == "TestRouteFullForCrea" 
                    && dbWayTest.Way_Prm.Count == 1 && dbWayTest.Step.Count ==2);

                var dbParams = dbWayTest.Way_Prm.ToList();

                Assert.IsTrue(dbParams[0].Prm_Name == "time" && dbParams[0].Prm_Value == "20");

                var dbSteps = dbWayTest.Step.ToList();

                Assert.IsTrue(dbSteps[0].Step_Name == "FirstStepFullForCrea");

                var dbFields1 = dbSteps[0].Step_Field.ToList();
                var dbStPrm1 = dbSteps[0].Step_Prm.ToList();
                Assert.IsTrue(dbFields1[0].Field_Name == "TestFieldFullForCrea" && dbFields1[0].Field_Type == 1
                && dbFields1[0].Editable_Falg == "1"
                    && dbFields1[0].Visible_Flag == "1" &&

                    dbStPrm1[0].Prm_Name == "TimeTestFullForCrea" && dbStPrm1[0].Prm_Value == "20");

                var dbFields2 = dbSteps[1].Step_Field.ToList();
                var dbStPrm2 = dbSteps[1].Step_Prm.ToList();
                Assert.IsTrue(dbFields2[0].Field_Name == "TestFieldFullForCrea1" && dbFields2[0].Field_Type == 1
                && dbFields2[0].Editable_Falg == "1"
                    && dbFields2[0].Visible_Flag == "1" &&

                    dbStPrm2[0].Prm_Name == "TimeTestFullForCrea1" && dbStPrm2[0].Prm_Value == "20");

                adfacade.DeleteRoute(dbWayTest.Way_ID);
            }
        }

        [Test]
        public void UpdateRoute_without_steps()
        {
            var adfacade = new AdminFacadeSql();

            RouteModel routeTest = new RouteModel();
            routeTest.RouteName = "TestRouteFullForCrea";

            routeTest.WayParams = new List<Way_Prm>();

            Way_Prm wayPrmTest1 = new Way_Prm();
            wayPrmTest1.Prm_Name = "time";
            wayPrmTest1.Prm_Value = "20";
            routeTest.WayParams.Add(wayPrmTest1);

            PointModel pointTest = new PointModel();

            Step stepTest = new Step();
            stepTest.Step_Name = "FirstStepFullForCrea";

            pointTest.Step = stepTest;

            Step_Prm stepPrmTest = new Step_Prm();
            stepPrmTest.Prm_Name = "TimeTestFullForCrea";
            stepPrmTest.Prm_Value = "20";

            pointTest.Params.Add(stepPrmTest);

            routeTest.Points.Add(pointTest);

            PointModel pointTest1 = new PointModel();

            Step stepTest1 = new Step();
            stepTest1.Step_Name = "FirstStepFullForCrea1";
            //stepTest.Step_Nmbr = 1;

            pointTest1.Step = stepTest1;

            Step_Prm stepPrmTest1 = new Step_Prm();
            stepPrmTest1.Prm_Name = "TimeTestFullForCrea1";
            stepPrmTest1.Prm_Value = "20";

            pointTest1.Params.Add(stepPrmTest1);

            routeTest.Points.Add(pointTest1);


            using (var context = new tpksEntities())
            {
                for (int i = 1144; i < 1200; i++)
                {
                    Way forDel = context.Way.Find(i);
                    if (forDel != null)
                    {
                        if (forDel.Way_Name == "TestRouteFullForCrea")
                        {
                            adfacade.DeleteRoute(i);
                        }
                    }
                }

                int res = adfacade.CreateRoute(routeTest);

                var allWays = context.Way.ToList();

                Way dbWayTest =
                    context.Way.Include("Way_Prm").Include("Step").FirstOrDefault(x => x.Way_Name == "TestRouteFullForCrea");



            routeTest = new RouteModel();
            routeTest.RouteName = "TestRouteFullForCrea";
                routeTest.RouteId = dbWayTest.Way_ID;

            routeTest.WayParams = new List<Way_Prm>();

            wayPrmTest1 = new Way_Prm();
            wayPrmTest1.Prm_Name = "time";
            wayPrmTest1.Prm_Value = "20";
            routeTest.WayParams.Add(wayPrmTest1);


                res = adfacade.CreateRoute(routeTest);


                dbWayTest =
                    context.Way.Include("Way_Prm").Include("Step").FirstOrDefault(x => x.Way_Name == "TestRouteFullForCrea");

                Assert.IsTrue(res == 0 && dbWayTest.Way_Name == "TestRouteFullForCrea"
                    && dbWayTest.Way_Prm.Count == 1 && dbWayTest.Step.Count == 0);

                var dbParams = dbWayTest.Way_Prm.ToList();

                Assert.IsTrue(dbParams[0].Prm_Name == "time" && dbParams[0].Prm_Value == "20");

                adfacade.DeleteRoute(dbWayTest.Way_ID);
            }
        }

        [Test]
        public void UpdateRoute_with_steps_without_fields()
        {
            var adfacade = new AdminFacadeSql();

            RouteModel routeTest = new RouteModel();
            routeTest.RouteName = "TestRouteFullForCrea";

            routeTest.WayParams = new List<Way_Prm>();

            Way_Prm wayPrmTest1 = new Way_Prm();
            wayPrmTest1.Prm_Name = "time";
            wayPrmTest1.Prm_Value = "20";
            routeTest.WayParams.Add(wayPrmTest1);

            using (var context = new tpksEntities())
            {
                for (int i = 1144; i < 1200; i++)
                {
                    Way forDel = context.Way.Find(i);
                    if (forDel != null)
                    {
                        if (forDel.Way_Name == "TestRouteFullForCrea")
                        {
                            adfacade.DeleteRoute(i);
                        }
                    }
                }

                int res = adfacade.CreateRoute(routeTest);

                var allWays = context.Way.ToList();

                Way dbWayTest =
                    context.Way.Include("Way_Prm").Include("Step").FirstOrDefault(x => x.Way_Name == "TestRouteFullForCrea");


            routeTest = new RouteModel();
            routeTest.RouteName = "TestRouteFullForCrea";
                routeTest.RouteId = dbWayTest.Way_ID;

            routeTest.WayParams = new List<Way_Prm>();

            wayPrmTest1 = new Way_Prm();
            wayPrmTest1.Prm_Name = "time";
            wayPrmTest1.Prm_Value = "20";
            routeTest.WayParams.Add(wayPrmTest1);

            PointModel pointTest = new PointModel();

            Step stepTest = new Step();
            stepTest.Step_Name = "FirstStepFullForCrea";

            pointTest.Step = stepTest;

            Step_Prm stepPrmTest = new Step_Prm();
            stepPrmTest.Prm_Name = "TimeTestFullForCrea";
            stepPrmTest.Prm_Value = "20";

            pointTest.Params.Add(stepPrmTest);

            routeTest.Points.Add(pointTest);

            PointModel pointTest1 = new PointModel();

            Step stepTest1 = new Step();
            stepTest1.Step_Name = "FirstStepFullForCrea1";
            //stepTest.Step_Nmbr = 1;

            pointTest1.Step = stepTest1;

            Step_Prm stepPrmTest1 = new Step_Prm();
            stepPrmTest1.Prm_Name = "TimeTestFullForCrea1";
            stepPrmTest1.Prm_Value = "20";

            pointTest1.Params.Add(stepPrmTest1);

            routeTest.Points.Add(pointTest1);


                res = adfacade.CreateRoute(routeTest);

                dbWayTest =
                    context.Way.Include("Way_Prm").Include("Step").FirstOrDefault(x => x.Way_Name == "TestRouteFullForCrea");

                Assert.IsTrue(res == 0 && dbWayTest.Way_Name == "TestRouteFullForCrea"
                    && dbWayTest.Way_Prm.Count == 1 && dbWayTest.Step.Count == 2);

                var dbParams = dbWayTest.Way_Prm.ToList();

                Assert.IsTrue(dbParams[0].Prm_Name == "time" && dbParams[0].Prm_Value == "20");

                var dbSteps = dbWayTest.Step.ToList();

                Assert.IsTrue(dbSteps[0].Step_Name == "FirstStepFullForCrea");

                var dbFields1 = dbSteps[0].Step_Field.ToList();
                var dbStPrm1 = dbSteps[0].Step_Prm.ToList();

                Assert.IsTrue(dbFields1.Count == 0 &&
                    dbStPrm1[0].Prm_Name == "TimeTestFullForCrea" && dbStPrm1[0].Prm_Value == "20");

                var dbFields2 = dbSteps[1].Step_Field.ToList();
                var dbStPrm2 = dbSteps[1].Step_Prm.ToList();
                Assert.IsTrue(dbFields2.Count == 0 && 
                    dbStPrm2[0].Prm_Name == "TimeTestFullForCrea1" && dbStPrm2[0].Prm_Value == "20");

                adfacade.DeleteRoute(dbWayTest.Way_ID);
            }
        }

        #endregion

        #region GetRouteById
        [Test]
        public void GetRouteById_not_exists()
        {
            using (var context = new tpksEntities())
            {
                var adfacade = new AdminFacadeSql();

                RouteModel res = adfacade.GetRouteById(999);

                Assert.IsTrue(res.RouteId == -2
                    );
            }

        }

        [Test]
        public void GetRouteById_exists()
        {
            using (var context = new tpksEntities())
            {
                Way wayTest = new Way();
                wayTest.Way_Name = "testWay";
                wayTest.Creation_Date = DateTime.Now;
                context.Way.Add(wayTest);
                context.SaveChanges();

                int idWay = wayTest.Way_ID;

                Way_Prm wayPrmTest = new Way_Prm();
                wayPrmTest.Way_ID = idWay;
                wayPrmTest.Prm_Name = "TimeTest";
                wayPrmTest.Prm_Value = "20";
                wayPrmTest.Field_Type = 1;
                context.Way_Prm.Add(wayPrmTest);
                context.SaveChanges();

                int idWayPrm = wayPrmTest.Cntr;

                Step stepTest = new Step();
                stepTest.Step_Name = "First step";
                stepTest.Way_ID = idWay;
                stepTest.Step_Nmbr = 1;
                context.Step.Add(stepTest);
                context.SaveChanges();

                int idStep = stepTest.Step_ID;

                Step_Prm stepPrmTest = new Step_Prm();
                stepPrmTest.Prm_Name = "TimeTest";
                stepPrmTest.Prm_Value = "20";
                stepPrmTest.Field_Type = 1;
                stepPrmTest.Step_ID = idStep;
                context.Step_Prm.Add(stepPrmTest);
                context.SaveChanges();

                int idStepPrm = stepPrmTest.Cntr;

                Step_Field stepFieldTest = new Step_Field();
                stepFieldTest.Step_ID = idStep;
                stepFieldTest.Field_Name = "TestField";
                stepFieldTest.Field_Type = 1;
                stepFieldTest.Field_Value = "TestValue";
                stepFieldTest.Visible_Flag = "1";
                stepFieldTest.Editable_Falg = "1";
                context.Step_Field.Add(stepFieldTest);
                context.SaveChanges();

                int idStepField = stepFieldTest.Cntr;


                var adfacade = new AdminFacadeSql();

                RouteModel way1 = adfacade.GetRouteById(1102);

                var allWays = context.Way.ToList();

                RouteModel res = adfacade.GetRouteById(idWay);

                Assert.IsTrue(res.RouteId == idWay && res.RouteName == "testWay" && res.WayParams.Count == 1 &&
                    res.WayParams[0].Cntr == idWayPrm && res.WayParams[0].Prm_Name == "TimeTest" && res.WayParams[0].Prm_Value == "20" && res.WayParams[0].Field_Type == 1 &&
                    res.Points.Count == 1 && res.Points[0].Step.Step_ID == idStep && res.Points[0].Step.Step_Name == "First step" && res.Points[0].Step.Step_Nmbr == 1 && res.Points[0].Step.Way_ID == idWay &&
                    res.Points[0].Params.Count == 1 && res.Points[0].Params[0].Cntr == idStepPrm && res.Points[0].Params[0].Field_Type == 1 && res.Points[0].Params[0].Prm_Name == "TimeTest" && res.Points[0].Params[0].Prm_Value == "20" && res.Points[0].Params[0].Step_ID == idStep &&
                    res.Points[0].Fields.Count == 1 && res.Points[0].Fields[0].Cntr == idStepField &&
                    res.Points[0].Fields[0].Step_ID == idStep &&
                    res.Points[0].Fields[0].Field_Name == "TestField" &&
                    res.Points[0].Fields[0].Field_Type == 1 &&
                    res.Points[0].Fields[0].Field_Value == "TestValue" &&
                    res.Points[0].Fields[0].Editable_Falg == "1" &&
                    res.Points[0].Fields[0].Visible_Flag == "1"
                    );

                adfacade.DeleteRoute(idWay);
            }

        }

        #endregion

        #region GetFilteredRoutes

        [Test]
        public void GetFilteredRoutes_orderenum_0()
        {
        var adfacade = new core.AdminFacadeSql();
        using (var context = new tpksEntities())
        {
            RouteFilter_l filter1 = new RouteFilter_l();
            filter1.OrderEnum = (RouteOrderEnum)0;
            filter1.StartIndex = 1;
            filter1.Count=9;
            filter1.IsOrderDesc = null;
            filter1.SearchString = "";
            List<RouteModel> list1 = new List<RouteModel>();
            list1.Add(adfacade.GetRouteById(1));
            list1.Add(adfacade.GetRouteById(2));
            list1.Add(adfacade.GetRouteById(3));
            list1.Add(adfacade.GetRouteById(4));
            list1.Add(adfacade.GetRouteById(5));
            list1.Add(adfacade.GetRouteById(6));
            list1.Add(adfacade.GetRouteById(7));
            list1.Add(adfacade.GetRouteById(8));
            list1.Add(adfacade.GetRouteById(9));

            List<RouteModel> list2 = adfacade.GetFilteredRoutes(filter1);
            Assert.IsTrue(list2[0].RouteId == 2 && list2[1].RouteId == 3 && list2[2].RouteId == 4 && list2[3].RouteId == 5 && list2[4].RouteId == 6 && list2[5].RouteId == 7 && list2[6].RouteId == 8 && list2[7].RouteId == 9 && list2[8].RouteId == 1001 && list2.Count == 9);
        }
    }

        [Test]
        public void GetFilteredRoutes_orderenum_1 ()
        {
            var adfacade = new core.AdminFacadeSql();
            using (var context = new tpksEntities())
            {
                RouteFilter_l filter1 = new RouteFilter_l();
                filter1.OrderEnum = (RouteOrderEnum)1;
                filter1.StartIndex = 1;
                filter1.Count = 9;
                filter1.IsOrderDesc = false;
                filter1.SearchString = "";
                List<RouteModel> list1 = new List<RouteModel>();
                list1.Add(adfacade.GetRouteById(1));
                list1.Add(adfacade.GetRouteById(2));
                list1.Add(adfacade.GetRouteById(3));
                list1.Add(adfacade.GetRouteById(4));
                list1.Add(adfacade.GetRouteById(5));
                list1.Add(adfacade.GetRouteById(6));
                list1.Add(adfacade.GetRouteById(7));
                list1.Add(adfacade.GetRouteById(8));
                list1.Add(adfacade.GetRouteById(9));

                List<RouteModel> list2 = adfacade.GetFilteredRoutes(filter1);
                Assert.IsTrue(list2[0].RouteId == 4 && list2[1].RouteId == 5 && list2[2].RouteId == 6 && list2[3].RouteId == 7 && list2[4].RouteId == 8 && list2[5].RouteId == 9 && list2[6].RouteId == 3 && list2[7].RouteId == 2 && list2[8].RouteId == 1 && list2.Count == 9);
            }
        }

        [Test]
        public void GetFilteredRoutes_orderenum_0_podtstroka_y()
        {
            var adfacade = new core.AdminFacadeSql();
            using (var context = new tpksEntities())
            {
                RouteFilter_l filter1 = new RouteFilter_l();
                filter1.OrderEnum = (RouteOrderEnum)1;
                filter1.StartIndex = 30;
                filter1.Count = 9;
                filter1.IsOrderDesc = null;
                filter1.SearchString = "y";
                List<RouteModel> list1 = new List<RouteModel>();
                list1.Add(adfacade.GetRouteById(1));
                list1.Add(adfacade.GetRouteById(2));
                list1.Add(adfacade.GetRouteById(3));
                list1.Add(adfacade.GetRouteById(4));
                list1.Add(adfacade.GetRouteById(5));
                list1.Add(adfacade.GetRouteById(6));
                list1.Add(adfacade.GetRouteById(7));
                list1.Add(adfacade.GetRouteById(8));
                list1.Add(adfacade.GetRouteById(9));

                List<RouteModel> list2 = adfacade.GetFilteredRoutes(filter1);
                Assert.IsTrue(list2[0].RouteId == 4 && list2[1].RouteId == 5 && list2[2].RouteId == 6 && list2[3].RouteId == 7 && list2[4].RouteId == 8 && list2[5].RouteId == 9 && list2.Count == 6);
            }
        }

        [Test]
        public void GetFilteredRoutes_orderenum_1_podtstroka_a()
        {
            var adfacade = new core.AdminFacadeSql();
            using (var context = new tpksEntities())
            {
                RouteFilter_l filter1 = new RouteFilter_l();
                filter1.OrderEnum = (RouteOrderEnum)1;
                filter1.StartIndex = 1;
                filter1.Count = 9;
                filter1.IsOrderDesc = false;
                filter1.SearchString = "a";
                List<RouteModel> list1 = new List<RouteModel>();
                list1.Add(adfacade.GetRouteById(1));
                list1.Add(adfacade.GetRouteById(2));
                list1.Add(adfacade.GetRouteById(3));
                list1.Add(adfacade.GetRouteById(4));
                list1.Add(adfacade.GetRouteById(5));
                list1.Add(adfacade.GetRouteById(6));
                list1.Add(adfacade.GetRouteById(7));
                list1.Add(adfacade.GetRouteById(8));
                list1.Add(adfacade.GetRouteById(9));

                List<RouteModel> list2 = adfacade.GetFilteredRoutes(filter1);
                Assert.IsTrue(list2[0].RouteId == 4 && list2[1].RouteId == 5 && list2[2].RouteId == 6 && list2[3].RouteId == 7 && list2[4].RouteId == 8 && list2[5].RouteId == 9 && list2[6].RouteId==1 &&list2.Count == 7);
            }
        }

        [Test]
        public void GetFilteredRoutes_orderenum_1_podtstroka_ay()
        {
            var adfacade = new core.AdminFacadeSql();
            using (var context = new tpksEntities())
            {
                RouteFilter_l filter1 = new RouteFilter_l();
                filter1.OrderEnum = (RouteOrderEnum)1;
                filter1.StartIndex = 1;
                filter1.Count = 9;
                filter1.IsOrderDesc = null;
                filter1.SearchString = "ay";
                List<RouteModel> list1 = new List<RouteModel>();
                list1.Add(adfacade.GetRouteById(1));
                list1.Add(adfacade.GetRouteById(2));
                list1.Add(adfacade.GetRouteById(3));
                list1.Add(adfacade.GetRouteById(4));
                list1.Add(adfacade.GetRouteById(5));
                list1.Add(adfacade.GetRouteById(6));
                list1.Add(adfacade.GetRouteById(7));
                list1.Add(adfacade.GetRouteById(8));
                list1.Add(adfacade.GetRouteById(9));

                List<RouteModel> list2 = adfacade.GetFilteredRoutes(filter1);
                Assert.IsTrue(list2[0].RouteId == 4 && list2[1].RouteId == 5 && list2[2].RouteId == 6 && list2[3].RouteId == 7 && list2[4].RouteId == 8 && list2[5].RouteId == 9 && list2.Count == 6);
            }
        }

        #endregion

        #endregion

        #region Docs

        #region CreateDoc

        [Test]
        public void CreateDoc()
        {
            var docfacade = new DocumentFacadeSql();
            Doc TestDoc = new Doc();
            DocModel TestModel = new DocModel();
            TestDoc.Doc_Type = 1;
            TestDoc.Doc_Name = "Auto";
            TestDoc.To_Doc_Path = "t_Test";
            TestDoc.Way_ID = 1;
            TestDoc.Current_Step = 2;
            TestDoc.Current_User_ID = 1133;
            TestDoc.Owner_ID = 1133;
            TestDoc.Full_Reject_Flag = "32";

            TestDoc.Create_Date = new DateTime(2010,03,03);
            
            TestModel.doc = TestDoc;
            TestModel.fields = new List<DocField>();
            TestModel.accept = false;
            TestModel.decline = false;

            docfacade.CreateDoc(TestModel);

            using (var context = new tpksEntities())
            {
                Doc testDoc1 = context.Doc.Include("Doc_Data").FirstOrDefault(x=>x.Doc_ID==12346);

                Assert.IsTrue(testDoc1.Doc_Name == TestDoc.Doc_Name &&
                              testDoc1.Doc_Type == TestDoc.Doc_Type
                              && testDoc1.To_Doc_Path == TestDoc.To_Doc_Path && testDoc1.Way_ID == TestDoc.Way_ID &&
                              testDoc1.Current_Step == TestDoc.Current_Step
                              && testDoc1.Current_User_ID == TestDoc.Current_User_ID &&
                              testDoc1.Owner_ID == TestDoc.Owner_ID && testDoc1.Create_Date == TestDoc.Create_Date &&
                              testDoc1.Full_Reject_Flag == TestDoc.Full_Reject_Flag);
            }


        }


        #endregion

        #region DeleteDoc

        [Test]
        public void DeleteDoc_()
        {
            var docFacade = new DocumentFacadeSql();

            using (var context = new tpksEntities())
            {

                Doc testDoc = new Doc();
                testDoc.Doc_Type = 1;
                testDoc.Doc_Name = "testDocForDel";
                testDoc.To_Doc_Path = "toNext";
                testDoc.Way_ID = 1001;
                testDoc.Current_Step = 1;
                testDoc.Current_User_ID = 1135;
                testDoc.Owner_ID = 1135;
                testDoc.Create_Date = DateTime.Now;
                testDoc.Full_Reject_Flag = "2";

                context.Doc.Add(testDoc);
                context.SaveChanges();

                int id_doc = testDoc.Doc_ID;
                
                Doc_Data testData = new Doc_Data();
                testData.Doc_ID = id_doc;
                testData.Title = "PRICE";
                testData.Value = "13";
                testData.Field_Type = 1;

                context.Doc_Data.Add(testData);
                context.SaveChanges();

                int id_data = testData.Cntr;

                wne.Context.History testHist = new wne.Context.History();
                testHist.Doc_ID = id_doc;
                testHist.User_ID = 1135;
                testHist.Step_Nmbr = 1;
                testHist.Date = DateTime.Now;
                testHist.End_Flag = "f";
                
                context.History.Add(testHist);
                context.SaveChanges();

                int id_hist = testHist.Cntr;

                Migration_Info testMigr = new Migration_Info();
                testMigr.Doc_ID = id_doc;
                testMigr.Outer_Server_Doc_ID = 12348;
                testMigr.Owner_Server = "testServ";
                testMigr.Doc_Creator = 12348;
                testMigr.Migration_Data = DateTime.Now;
                testMigr.Add_Info = "testInfo";

                context.Migration_Info.Add(testMigr);
                context.SaveChanges();

                int id_migr = testMigr.Cntr;



                docFacade.DeleteDoc(id_doc);

                Doc doc1 = context.Doc.Find(id_doc);

                Assert.IsTrue(doc1 == null);

            }
        }

        #endregion

        #region GetFilteredDocs + GetFilteredDocsAvailable + GetFilteredDocsDone

        [Test]
        public void GetFilteredDocs_User_without_docs()
        {
            var docfacade = new DocumentFacadeSql();

            DocFilter filt = new DocFilter();
            filt.OrderEnum = DocOrderEnum.Nil;
            filt.SearchString = "";

            List<Smalldoc> list1 = docfacade.GetFilteredDocs(filt, 1136);
            List<Smalldoc> list2 = docfacade.GetFilteredDocsAvailable(filt, 1136);
            List<Smalldoc> list3 = docfacade.GetFilteredDocsDone(filt, 1136);

            Assert.IsTrue(list1.Count == 0 && list2.Count == 0 && list3.Count == 0);
        }

        [Test]
        public void GetFilteredDocs_User_with_docs_is_not_right()
        {
            var docfacade = new DocumentFacadeSql();

            DocFilter filt = new DocFilter();
            filt.OrderEnum = DocOrderEnum.Nil;
            filt.SearchString = "";

            List<Smalldoc> list1 = docfacade.GetFilteredDocs(filt, 1049);
            List<Smalldoc> list2 = docfacade.GetFilteredDocsAvailable(filt, 1050);
            List<Smalldoc> list3 = docfacade.GetFilteredDocsDone(filt, 1050);

            Assert.IsTrue(list1.Count == 0 && list2.Count == 0 && list3.Count == 0);
        }

        [Test]
        public void GetFilteredDocs_User_with_docs_is_right()
        {
            var docfacade = new DocumentFacadeSql();

            DocFilter filt = new DocFilter();
            filt.OrderEnum = DocOrderEnum.Nil;
            filt.SearchString = "";

            List<Smalldoc> list1 = docfacade.GetFilteredDocs(filt, 1049);
            List<Smalldoc> list2 = docfacade.GetFilteredDocsAvailable(filt, 1050);
            List<Smalldoc> list3 = docfacade.GetFilteredDocsDone(filt, 1050);

            Assert.IsTrue(list1.Count == 0 && list2.Count == 0 && list3.Count == 0);
        }

        [Test]
        public void GetFilteredDocsDone_User_with_docs()
        {
            var docfacade = new DocumentFacadeSql();

            DocFilter filt1 = new DocFilter();
            filt1.OrderEnum = DocOrderEnum.Nil;
            filt1.IsOrderDesc = true;
            filt1.SearchString = "";

            List<Smalldoc> list1 = docfacade.GetFilteredDocsDone(filt1, 1135);


            Assert.IsTrue(list1.Count == 3 && list1[0].id == 12348 && list1[1].id == 12349 && list1[2].id == 12351);

            DocFilter filt2 = new DocFilter();
            filt2.OrderEnum = DocOrderEnum.Name;
            filt2.StartIndex = 1;
            filt2.IsOrderDesc = true;
            filt2.SearchString = "";

            List<Smalldoc> list2 = docfacade.GetFilteredDocsDone(filt2, 1135);

            Assert.IsTrue(list2.Count == 2 && list2[0].id == 12348 && list2[1].id == 12349);

            DocFilter filt3 = new DocFilter();
            filt3.OrderEnum = DocOrderEnum.Name;
            filt3.IsOrderDesc = false;
            filt3.SearchString = "";

            List<Smalldoc> list3 = docfacade.GetFilteredDocsDone(filt3, 1135);

            Assert.IsTrue(list3.Count == 3 && list3[0].id == 12349 && list3[1].id == 12348 && list3[2].id == 12351);

            DocFilter filt4 = new DocFilter();
            filt4.OrderEnum = DocOrderEnum.StartTime;
            filt4.IsOrderDesc = false;
            filt4.SearchString = "";

            List<Smalldoc> list4 = docfacade.GetFilteredDocsDone(filt4, 1135);

            Assert.IsTrue(list4.Count == 3 && list4[0].id == 12348 && list4[1].id == 12349 && list4[2].id == 12351);

            DocFilter filt5 = new DocFilter();
            filt5.OrderEnum = DocOrderEnum.StartTime;
            filt5.IsOrderDesc = true;
            filt5.SearchString = "Z";

            List<Smalldoc> list5 = docfacade.GetFilteredDocsDone(filt4, 1135);

            Assert.IsTrue(list5.Count == 1 && list5[0].id == 12349 );

        }

        [Test]
        public void GetFilteredDocsavail_User_with_docs()
        {
            var docfacade = new DocumentFacadeSql();

            DocFilter filt1 = new DocFilter();
            filt1.OrderEnum = DocOrderEnum.Nil;
            filt1.IsOrderDesc = true;
            filt1.SearchString = "";

            List<Smalldoc> list1 = docfacade.GetFilteredDocsDone(filt1, 1135);


            Assert.IsTrue(list1.Count == 3 && list1[0].id == 12348 && list1[1].id == 12349 && list1[2].id == 12351);

            DocFilter filt2 = new DocFilter();
            filt2.OrderEnum = DocOrderEnum.Name;
            filt2.StartIndex = 1;
            filt2.IsOrderDesc = true;
            filt2.SearchString = "";

            List<Smalldoc> list2 = docfacade.GetFilteredDocsDone(filt2, 1135);

            Assert.IsTrue(list2.Count == 2 && list2[0].id == 12348 && list2[1].id == 12349);

            DocFilter filt3 = new DocFilter();
            filt3.OrderEnum = DocOrderEnum.Name;
            filt3.IsOrderDesc = false;
            filt3.SearchString = "";

            List<Smalldoc> list3 = docfacade.GetFilteredDocsDone(filt3, 1135);

            Assert.IsTrue(list3.Count == 3 && list3[0].id == 12349 && list3[1].id == 12348 && list3[2].id == 12351);

            DocFilter filt4 = new DocFilter();
            filt4.OrderEnum = DocOrderEnum.StartTime;
            filt4.IsOrderDesc = false;
            filt4.SearchString = "";

            List<Smalldoc> list4 = docfacade.GetFilteredDocsDone(filt4, 1135);

            Assert.IsTrue(list4.Count == 3 && list4[0].id == 12348 && list4[1].id == 12349 && list4[2].id == 12351);

            DocFilter filt5 = new DocFilter();
            filt5.OrderEnum = DocOrderEnum.StartTime;
            filt5.IsOrderDesc = true;
            filt5.SearchString = "Z";

            List<Smalldoc> list5 = docfacade.GetFilteredDocsDone(filt4, 1135);

            Assert.IsTrue(list5.Count == 1 && list5[0].id == 12349);

        }

        [Test]
        public void GetFilteredDocs_User_with_docs()
        {
            var docfacade = new DocumentFacadeSql();

            DocFilter filt1 = new DocFilter();
            filt1.OrderEnum = DocOrderEnum.Nil;
            filt1.IsOrderDesc = true;
            filt1.SearchString = "";

            List<Smalldoc> list1 = docfacade.GetFilteredDocsDone(filt1, 1135);


            Assert.IsTrue(list1.Count == 3 && list1[0].id == 12348 && list1[1].id == 12349 && list1[2].id == 12351);

            DocFilter filt2 = new DocFilter();
            filt2.OrderEnum = DocOrderEnum.Name;
            filt2.StartIndex = 1;
            filt2.IsOrderDesc = true;
            filt2.SearchString = "";

            List<Smalldoc> list2 = docfacade.GetFilteredDocsDone(filt2, 1135);

            Assert.IsTrue(list2.Count == 2 && list2[0].id == 12348 && list2[1].id == 12349);

            DocFilter filt3 = new DocFilter();
            filt3.OrderEnum = DocOrderEnum.Name;
            filt3.IsOrderDesc = false;
            filt3.SearchString = "";

            List<Smalldoc> list3 = docfacade.GetFilteredDocsDone(filt3, 1135);

            Assert.IsTrue(list3.Count == 3 && list3[0].id == 12349 && list3[1].id == 12348 && list3[2].id == 12351);

            DocFilter filt4 = new DocFilter();
            filt4.OrderEnum = DocOrderEnum.StartTime;
            filt4.IsOrderDesc = false;
            filt4.SearchString = "";

            List<Smalldoc> list4 = docfacade.GetFilteredDocsDone(filt4, 1135);

            Assert.IsTrue(list4.Count == 3 && list4[0].id == 12348 && list4[1].id == 12349 && list4[2].id == 12351);

            DocFilter filt5 = new DocFilter();
            filt5.OrderEnum = DocOrderEnum.StartTime;
            filt5.IsOrderDesc = true;
            filt5.SearchString = "Z";

            List<Smalldoc> list5 = docfacade.GetFilteredDocsDone(filt4, 1135);

            Assert.IsTrue(list5.Count == 1 && list5[0].id == 12349);

        }
        #endregion

        #region GetFullDoc

        [Test]
        public void GetFullDoc()
        {
            var docFacade = new DocumentFacadeSql();
            using (var context = new tpksEntities())
            {
                Doc doc1 = context.Doc.Find(1234);
                Fulldoc fulldoc1 = docFacade.GetFullDoc(1234);
                Assert.IsTrue(fulldoc1.now.doc.Create_Date == doc1.Create_Date && fulldoc1.Route.RouteId == doc1.Way_ID && fulldoc1.Route.RouteName == doc1.Way.Way_Name && fulldoc1.MigrationInfo.SequenceEqual(doc1.Migration_Info));

            }

        }

        [Test]
        public void GetFullDoc_null()
        {
            var docFacade = new DocumentFacadeSql();
            using (var context = new tpksEntities())
            {

                Assert.IsNull(docFacade.GetFullDoc(1597));

            }

        }

        #endregion
        #region AddOutDoc

        [Test]
        public void AddOutDoc_not_new_id()
        {
            var docFacade = new DocumentFacadeSql();
            using (var context = new tpksEntities())
            {
                Fulldoc fulldoc1 = docFacade.GetFullDoc(1234);
                docFacade.AddOutDoc(fulldoc1);

            }
        }
        [Test]
        public void AddOutDoc_new_id()
        {
            var docFacade = new DocumentFacadeSql();
            using (var context = new tpksEntities())
            {
                Fulldoc fulldoc1 = docFacade.GetFullDoc(1234);
                fulldoc1.now.doc.Doc_ID = 1654;
                docFacade.AddOutDoc(fulldoc1);
                Doc doc1 = context.Doc.Find(1);
                Doc doc2 = context.Doc.Find(1234);
                Assert.IsTrue(doc1.Doc_Name == doc2.Doc_Name);
                context.Doc.Remove(doc1);
            }
        }

        #endregion
        #region  SaveFileToDoc

        [Test]
        public void SaveFileToDoc_1()
        {
            var docFacade = new DocumentFacadeSql();
            using (var context = new tpksEntities())
            {
                byte[] byte1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
                string string1 = "D:/WNE/WNE/wne.core/file1.txt";
                docFacade.SaveFileToDoc(1234,byte1,string1);
                Doc doc1 = context.Doc.Find(1234);
                Doc_Data date = new Doc_Data();
                date.Doc_ID = 1234;
                date.Field_Type = 7;
                date.Title = string1;
                date.Value = "Что-то";
                Assert.IsTrue(doc1.Doc_Data.Last().Doc_ID == 1234 && doc1.Doc_Data.Last().Field_Type == 7 && doc1.Doc_Data.Last().Value == "Что-то");
            }
        }
        #endregion
        #endregion
    }
}
